Check if given Parentheses expression is balanced or not
Given a string str of length N, consisting of ‘(‘ and ‘)‘ only, the task is to check whether it is balanced or not.
Examples:
Input: str = “((()))()()”
Output: BalancedInput: str = “())((())”
Output: Not Balanced
Approach:
 Declare a Flag variable which denotes expression is balanced or not.
 Initialise Flag variable with true and Count variable with 0.
 Traverse through the given expression

 If we encounter an opening parentheses (, increase count by 1
 If we encounter a closing parentheses ), decrease count by 1
 If Count becomes negative at any point, then expression is said to be not balanced,
so mark Flag as false and break from loop.
 After traversing the expression, if Count is not equal to 0,
it means the expression is not balanced so mark Flag as false.  Finally, if Flag is true, expression is balanced else not balanced.
Below is the implementation of the above approach:
C
// C program of the above approach #include <stdbool.h> #include <stdio.h> // Function to check if // parentheses are balanced bool isBalanced( char exp []) { // Initialising Variables bool flag = true ; int count = 0; // Traversing the Expression for ( int i = 0; exp [i] != '\0' ; i++) { if ( exp [i] == '(' ) { count++; } else { // It is a closing parenthesis count; } if (count < 0) { // This means there are // more Closing parenthesis // than opening ones flag = false ; break ; } } // If count is not zero, // It means there are more // opening parenthesis if (count != 0) { flag = false ; } return flag; } // Driver code int main() { char exp1[] = "((()))()()" ; if (isBalanced(exp1)) printf ( "Balanced \n" ); else printf ( "Not Balanced \n" ); char exp2[] = "())((())" ; if (isBalanced(exp2)) printf ( "Balanced \n" ); else printf ( "Not Balanced \n" ); return 0; } 
C++
// C++ program for the above approach. #include <bits/stdc++.h> using namespace std; // Function to check // if parentheses are balanced bool isBalanced(string exp ) { // Initialising Variables bool flag = true ; int count = 0; // Traversing the Expression for ( int i = 0; i < exp .length(); i++) { if ( exp [i] == '(' ) { count++; } else { // It is a closing parenthesis count; } if (count < 0) { // This means there are // more Closing parenthesis // than opening ones flag = false ; break ; } } // If count is not zero, // It means there are // more opening parenthesis if (count != 0) { flag = false ; } return flag; } // Driver code int main() { string exp1 = "((()))()()" ; if (isBalanced(exp1)) cout << "Balanced \n" ; else cout << "Not Balanced \n" ; string exp2 = "())((())" ; if (isBalanced(exp2)) cout << "Balanced \n" ; else cout << "Not Balanced \n" ; return 0; } 
Balanced Not Balanced
Time complexity: O(N)
Auxiliary Space: O(1)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a studentfriendly price.
Recommended Posts:
 A Program to check if strings are rotations of each other or not
 Check for balanced parentheses in an expression
 Check whether two strings are anagram of each other
 Check if a given string is a rotation of a palindrome
 Check a given sentence for a given set of simple grammer rules
 Check if a given sequence of moves for a robot is circular or not
 Check if two given strings are isomorphic to each other
 Minimum number of bracket reversals needed to make an expression balanced
 Check if edit distance between two strings is one
 std::regex_match, std::regex_replace()  Regex (Regular Expression) In C++
 Check if string follows order of characters defined by a pattern or not  Set 1
 Check if string follows order of characters defined by a pattern or not  Set 2
 Check if string follows order of characters defined by a pattern or not  Set 3
 Check whether a given number is even or odd
 Check if a string has all characters with same frequency with one variation allowed
 Check if a string can become empty by recursively deleting a given substring
 Check for Palindrome after every character replacement Query
 Check if a string can be obtained by rotating another string 2 places
 Check if all rows of a matrix are circular rotations of each other
 Check if two strings are kanagrams 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.