

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check for balanced parentheses in an expression in C++
Suppose we have an expression. The expression has some parentheses; we have to check the parentheses are balanced or not. The order of the parentheses are (), {} and []. Suppose there are two strings. “()[(){()}]” this is valid, but “{[}]” is invalid.
The task is simple; we will use stack to do this. We should follow these steps to get the solution −
- Traverse through the expression until it has exhausted
- if the current character is opening bracket like (, { or [, then push into stack
- if the current character is closing bracket like ), } or ], then pop from stack, and check whether the popped bracket is corresponding starting bracket of the current character, then it is fine, otherwise that is not balanced.
- After the string is exhausted, if there are some starting bracket left into the stack, then the string is not balanced.
Example
#include <iostream> #include <stack> using namespace std; bool isBalancedExp(string exp) { stack<char> stk; char x; for (int i=0; i<exp.length(); i++) { if (exp[i]=='('||exp[i]=='['||exp[i]=='{') { stk.push(exp[i]); continue; } if (stk.empty()) return false; switch (exp[i]) { case ')': x = stk.top(); stk.pop(); if (x=='{' || x=='[') return false; break; case '}': x = stk.top(); stk.pop(); if (x=='(' || x=='[') return false; break; case ']': x = stk.top(); stk.pop(); if (x =='(' || x == '{') return false; break; } } return (stk.empty()); } int main() { string expresion = "()[(){()}]"; if (isBalancedExp(expresion)) cout << "This is Balanced Expression"; else cout << "This is Not Balanced Expression"; }
Output
This is Balanced Expression
- Related Questions & Answers
- Check for balanced parentheses in Python
- Check for balanced parentheses in an expression - O(1) space - O(N^2) time complexity in C++
- Check for balanced parentheses in an expression O(1) space O(N^2) time complexity in Python
- Program to check whether parentheses are balanced or not in Python
- Count pairs of parentheses sequences such that parentheses are balanced in C++
- C++ Remove Invalid Parentheses from an Expression
- Print all combinations of balanced parentheses in C++
- C++ Balanced expression with replacement
- How to match parentheses in Python regular expression?
- Program to find maximum number of balanced groups of parentheses in Python
- C++ Program to Check for balanced paranthesis by using Stacks
- Print the balanced bracket expression using given brackets in C Program
- In MySQL, how to check for a pattern which is not present within an expression?
- Replace the Substring for Balanced String in C++
- C++ Program to Construct an Expression Tree for a Postfix Expression
Advertisements