
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Check for balanced parentheses in an expression - O(1) space - O(N^2) time complexity in C++
Concept
With respect of given a string str containing characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, the task is to find if brackets are balanced or not.
Brackets are denoted as balanced if −
We close open brackets must be closed by the same type of brackets.
Again we close open brackets according to the correct order.
Input − str = “(()){}”
Output − Yes
Input − str = “))(([][”
Output − No
Method
Assign two variables a and b to keep track of two brackets to be compared.
A count should be maintained whose value increments on encountering opening bracket and decrements on encountering a closing bracket.
Assign b = a, a = a + 1 and count=count+1 when opening brackets are encountered.
At the time when closing brackets are encountered decrement count and compare brackets at i and j,
If it has been seen that brackets at a and b are a match, then substitute ‘#’ in string at a th and b th position. a is incremented and b is decremented until non ‘#’ value is encountered or b ≥ 0.
If it has been seen that brackets at a and b are not a match then return false.
If count != 0 then return false.
Example
// C++ implementation of the approach #include <iostream> using namespace std; bool helperFunc(int& count1, string& s1, int& i1, int& j1, char tocom1){ count1--; if (j1 > -1 && s1[j1] == tocom1) { s1[i1] = '#'; s1[j1] = '#'; while (j1 >= 0 && s1[j1] == '#') j1--; i1++; return 1; } else return 0; } bool isValid(string s1){ if (s1.length() == 0) return true; else { int i1 = 0; int count1 = 0; int j1 = -1; bool result1; while (i1 < s1.length()) { switch (s1[i1]) { case '}': result1 = helperFunc(count1, s1, i1, j1, '{'); if (result1 == 0) { return false; } break; case ')': result1 = helperFunc(count1, s1, i1, j1, '('); if (result1 == 0) { return false; } break; case ']': result1 = helperFunc(count1, s1, i1, j1, '['); if (result1 == 0) { return false; } break; default: j1 = i1; i1++; count1++; } } if (count1 != 0) return false; return true; } } // Driver code int main(){ string str1 = "[[]][]()"; if (isValid(str1)) cout << "Yes"; else cout << "No"; return 0; }
Output
Yes
- Related Articles
- Check for balanced parentheses in an expression O(1) space O(N^2) time complexity in Python
- Check for balanced parentheses in an expression in C++
- Find duplicates in O(n) time and O(1) extra space - Set 1 in C++
- Find median of BST in O(n) time and O(1) space in Python
- Find median of BST in O(n) time and O(1) space in C++
- Find the maximum repeating number in O(n) time and O(1) extra space in Python
- Print left rotation of array in O(n) time and O(1) space in C Program.
- Rearrange positive and negative numbers in O(n) time and O(1) extra space in C++
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space in C++
- Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers) in Python
- Count frequencies of all elements in array in O(1) extra space and O(n) time in C++
- Find maximum in a stack in O(1) time and O(1) extra space in C++
- Check for balanced parentheses in Python
- Find duplicate in an array in O(n) and by using O(1) extra space in C++
- Print n x n spiral matrix using O(1) extra space in C Program.
