# Check for balanced parentheses in an expression | O(1) space | O(N^2) time complexity

Given a string **str** containing characters **‘(‘**, **‘)’**, **‘{‘**, **‘}’**, **‘[‘** and **‘]’**, the task is to determine if brackets are balanced or not.

Brackets are balanced if:

- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.

**Examples:**

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

Output:Yes

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

Output:No

**Approach:**

- Keep two variables
**i**and**j**to keep track of two brackets to be compared. - Maintain a count whose value increments on encountering opening bracket and decrements on encountering a closing bracket.
- Set
**j = i**,**i = i + 1**and**counter++**when opening brackets are encountered. - When Closing brackets are encountered decrement count and compare brackets at
**i**and**j**,- If brackets at
**i**and**j**are a match, then substitute**‘#’**in string at**i**and^{th}**j**position. Increment^{th}**i**and decrement**j**until non**‘#’**value is encountered or**j ≥ 0**. - If brackets at
**i**and**j**are not a match then return false.

- If brackets at
- If
**count != 0**then return false.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// This helper function is called whenever ` `// closing bracket is encountered. ` `// Hence count is decremented ` `// j and i points to opening and closing ` `// brackets to be matched respectively ` `// If brackets at i and j is a match ` `// replace them with "#" character and decrement j ` `// to point next opening bracket to * be matched ` `// Similarly, increment i to point to next closing ` `// bracket to be matched ` `// If j is out of bound or brackets did not match return 0 ` `bool` `helperFunc(` `int` `& count, string& s, ` `int` `& i, ` `int` `& j, ` `char` `tocom) ` `{ ` ` ` `count--; ` ` ` `if` `(j > -1 && s[j] == tocom) { ` ` ` `s[i] = ` `'#'` `; ` ` ` `s[j] = ` `'#'` `; ` ` ` `while` `(j >= 0 && s[j] == ` `'#'` `) ` ` ` `j--; ` ` ` `i++; ` ` ` `return` `1; ` ` ` `} ` ` ` `else` ` ` `return` `0; ` `} ` ` ` `// Function that returns true if s is a ` `// valid balanced bracket string ` `bool` `isValid(string s) ` `{ ` ` ` ` ` `// Empty string is considered balanced ` ` ` `if` `(s.length() == 0) ` ` ` `return` `true` `; ` ` ` ` ` `else` `{ ` ` ` `int` `i = 0; ` ` ` ` ` `// Increments for opening bracket and ` ` ` `// decrements for closing bracket ` ` ` `int` `count = 0; ` ` ` `int` `j = -1; ` ` ` `bool` `result; ` ` ` `while` `(i < s.length()) { ` ` ` `switch` `(s[i]) { ` ` ` `case` `'}'` `: ` ` ` `result = helperFunc(count, s, i, j, ` `'{'` `); ` ` ` `if` `(result == 0) { ` ` ` `return` `false` `; ` ` ` `} ` ` ` `break` `; ` ` ` ` ` `case` `')'` `: ` ` ` `result = helperFunc(count, s, i, j, ` `'('` `); ` ` ` `if` `(result == 0) { ` ` ` `return` `false` `; ` ` ` `} ` ` ` `break` `; ` ` ` ` ` `case` `']'` `: ` ` ` `result = helperFunc(count, s, i, j, ` `'['` `); ` ` ` `if` `(result == 0) { ` ` ` `return` `false` `; ` ` ` `} ` ` ` `break` `; ` ` ` ` ` `default` `: ` ` ` `j = i; ` ` ` `i++; ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// count != 0 indicates unbalanced parentheses ` ` ` `// this check is required to handle cases where ` ` ` `// count of opening brackets > closing brackets ` ` ` `if` `(count != 0) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str = ` `"[[]][]()"` `; ` ` ` ` ` `if` `(isValid(str)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Yes

## Recommended Posts:

- Check for balanced parentheses in an expression | O(1) space
- Check for balanced parentheses in an expression
- Check for balanced parentheses in Python
- Count pairs of parentheses sequences such that parentheses are balanced
- Print all combinations of balanced parentheses
- Length of longest balanced parentheses prefix
- Balanced expression with replacement
- Print the balanced bracket expression using given brackets
- Minimum number of bracket reversals needed to make an expression balanced | Set - 2
- Minimum number of bracket reversals needed to make an expression balanced
- Time complexity of all permutations of a string
- Sieve of Eratosthenes in 0(n) time complexity
- Space and time efficient Binomial Coefficient
- Case-specific sorting of Strings in O(n) time and O(1) space
- Find the largest multiple of 3 from array of digits | Set 2 (In O(n) time and O(1) space)

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.