# Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2

Given a bracket sequence as a string **str**, the task is to find whether the given string can be balanced by moving at most one bracket from its original place in the sequence to any other position.

**Examples:**

Input:str = “)(()”

Output:Yes

As by moving s[0] to the end will make it valid.

“(())”

Input:str = “()))(()”

Output:No

**Approach:** The problem can be solved using a stack as discussed in this post. In this article, an approach which doesn’t use extra space will be discussed.

If the frequency of ‘(‘ is less than frequency of ‘)’. If the above difference is greater than 1 then the sequence cannot be balanced else it can be balanced if the overall difference is zero.

Below is implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns true if ` `// the string can be balanced ` `bool` `canBeBalanced(string s, ` `int` `n) ` `{ ` ` ` ` ` `// Count to check the difference between ` ` ` `// the frequencies of '(' and ')' and ` ` ` `// count_1 is to find the minimum value ` ` ` `// of freq('(') - freq(')') ` ` ` `int` `count = 0, count_1 = 0; ` ` ` ` ` `// Traverse the given string ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// Increase the count ` ` ` `if` `(s[i] == ` `'('` `) ` ` ` `count++; ` ` ` ` ` `// Decrease the count ` ` ` `else` ` ` `count--; ` ` ` ` ` `// Find the minimum value ` ` ` `// of freq('(') - freq(')') ` ` ` `count_1 = min(count_1, count); ` ` ` `} ` ` ` ` ` `// If the minimum difference is greater ` ` ` `// than or equal to -1 and the overall ` ` ` `// difference is zero ` ` ` `if` `(count_1 >= -1 && count == 0) ` ` ` `return` `true` `; ` ` ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string s = ` `"())()("` `; ` ` ` `int` `n = s.length(); ` ` ` ` ` `if` `(canBeBalanced(s, n)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to toggle K-th bit of a number N ` `class` `GFG ` `{ ` ` ` `// Function that returns true if ` `// the string can be balanced ` `static` `boolean` `canBeBalanced(String s, ` `int` `n) ` `{ ` ` ` ` ` `// Count to check the difference between ` ` ` `// the frequencies of '(' and ')' and ` ` ` `// count_1 is to find the minimum value ` ` ` `// of freq('(') - freq(')') ` ` ` `int` `count = ` `0` `, count_1 = ` `0` `; ` ` ` ` ` `// Traverse the given string ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` ` ` `// Increase the count ` ` ` `if` `(s.charAt(i) == ` `'('` `) ` ` ` `count++; ` ` ` ` ` `// Decrease the count ` ` ` `else` ` ` `count--; ` ` ` ` ` `// Find the minimum value ` ` ` `// of freq('(') - freq(')') ` ` ` `count_1 = Math.min(count_1, count); ` ` ` `} ` ` ` ` ` `// If the minimum difference is greater ` ` ` `// than or equal to -1 and the overall ` ` ` `// difference is zero ` ` ` `if` `(count_1 >= -` `1` `&& count == ` `0` `) ` ` ` `return` `true` `; ` ` ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String []args) ` `{ ` ` ` `String s = ` `"())()("` `; ` ` ` `int` `n = s.length(); ` ` ` ` ` `if` `(canBeBalanced(s, n)) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function that returns true if ` `# the can be balanced ` `def` `canBeBalanced(s, n): ` ` ` ` ` `# Count to check the difference between ` ` ` `# the frequencies of '(' and ')' and ` ` ` `# count_1 is to find the minimum value ` ` ` `# of freq('(') - freq(')') ` ` ` `count ` `=` `0` ` ` `count_1 ` `=` `0` ` ` ` ` `# Traverse the given string ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# Increase the count ` ` ` `if` `(s[i] ` `=` `=` `'('` `): ` ` ` `count ` `+` `=` `1` ` ` ` ` `# Decrease the count ` ` ` `else` `: ` ` ` `count ` `-` `=` `1` ` ` ` ` `# Find the minimum value ` ` ` `# of freq('(') - freq(')') ` ` ` `count_1 ` `=` `min` `(count_1, count) ` ` ` ` ` `# If the minimum difference is greater ` ` ` `# than or equal to -1 and the overall ` ` ` `# difference is zero ` ` ` `if` `(count_1 >` `=` `-` `1` `and` `count ` `=` `=` `0` `): ` ` ` `return` `True` ` ` ` ` `return` `False` ` ` `# Driver code ` `s ` `=` `"())()("` `n ` `=` `len` `(s) ` ` ` `if` `(canBeBalanced(s, n)): ` ` ` `print` `(` `"Yes"` `) ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to toggle K-th bit of a number N ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function that returns true if ` `// the string can be balanced ` `static` `Boolean canBeBalanced(String s, ` `int` `n) ` `{ ` ` ` ` ` `// Count to check the difference between ` ` ` `// the frequencies of '(' and ')' and ` ` ` `// count_1 is to find the minimum value ` ` ` `// of freq('(') - freq(')') ` ` ` `int` `count = 0, count_1 = 0; ` ` ` ` ` `// Traverse the given string ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` ` ` `// Increase the count ` ` ` `if` `(s[i] == ` `'('` `) ` ` ` `count++; ` ` ` ` ` `// Decrease the count ` ` ` `else` ` ` `count--; ` ` ` ` ` `// Find the minimum value ` ` ` `// of freq('(') - freq(')') ` ` ` `count_1 = Math.Min(count_1, count); ` ` ` `} ` ` ` ` ` `// If the minimum difference is greater ` ` ` `// than or equal to -1 and the overall ` ` ` `// difference is zero ` ` ` `if` `(count_1 >= -1 && count == 0) ` ` ` `return` `true` `; ` ` ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `String s = ` `"())()("` `; ` ` ` `int` `n = s.Length; ` ` ` ` ` `if` `(canBeBalanced(s, n)) ` ` ` `Console.WriteLine(` `"Yes"` `); ` ` ` `else` ` ` `Console.WriteLine(` `"No"` `); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

Yes

## Recommended Posts:

- Convert an unbalanced bracket sequence to a balanced sequence
- Find index of closing bracket for a given opening bracket in an expression
- Print the balanced bracket expression using given brackets
- Number of balanced bracket subsequence of length 2 and 4
- Number of balanced bracket expressions that can be formed from a string
- Minimum number of bracket reversals needed to make an expression balanced
- Minimum number of bracket reversals needed to make an expression balanced | Set - 2
- Minimum sum possible of any bracket sequence of length N
- Number of closing brackets needed to complete a regular bracket sequence
- Print Bracket Number
- Expression contains redundant bracket or not
- Range Queries for Longest Correct Bracket Subsequence Set | 2
- Range Queries for Longest Correct Bracket Subsequence
- Construct Binary Tree from String with bracket representation
- InfyTQ 2019 : Find the position from where the parenthesis is not balanced
- Minimum operation required to make a balanced sequence
- Find the Kth position element of the given sequence
- Find element position in given monotonic sequence
- Check if the number is balanced
- Check if N is a Balanced Prime number or not

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.