Balanced Parentheses

Overview
Check for balanced parentheses in the expression
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Takeaways
Balanced parentheses means that each opening symbol has a corresponding closing symbol and the pairs of parentheses are properly nested.
Problem Statement
Given an input expression string of length n consisting of three types of parentheses - {,},(,),[,].Check for balanced parentheses in the expression (well-formedness) using Stack. Parentheses are balanced if:
- The same kind of parentheses are used to close any open ones.
- The proper sequence must be used to close any open parentheses.
Example
- Input: expression = ~([]){}[[(){}]{}] Output: Yes, Balanced
- Input: expression = [(])) Output: No, Not Balanced
- It’s real life application is during the compilation of any code

Example Explanation
-
As in example 1, there is every closing bracket according to their corresponding opening brackets so yes this is balanced parentheses.
-
In the second example, it is observable that the parentheses are not balanced because the ( at the 2nd position in the expression should be closed before the closing of the ].
-
In the 3rd example, there is no { for the } that is in line number 8 of the code. Due to this, an error will be raised if we will try to run the code.
Constraints
- All of characters in the sequence ∈ (,),{,},[,].
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Approach
We must make an observation in order to resolve this problem. The most recent opening parenthesis must coincide with the subsequent closing symbol as you read symbols from left to right. Additionally, it's possible that the very last symbol processed will be the one to match the first opening symbol. Opening and closing symbols are matched from the inside out, in the opposite order from which they first appear. This is a hint that the issue can be resolved using stacks.
Algorithm
- Declare an empty stack.
- Now start traversing the input string.
- If you come across an opening bracket while traversing the string, add it to the stack.
- Else the current character is a closing bracket. In this case, check to see if the top element of the stack is of the corresponding opening type and if it is then pop the top element from the stack and if not, then return false.
- If the stack is empty after traversing the string, return true; otherwise, return false.
Turn Learning into Career Growth
Code Implementation
In C++
In Java
In Python
Output
Time Complexity:
As we are traversing over the expression of length n only once using the for loop so O(n) will be it's time complexity.
Space Complexity:
As we are creating a new stack and the size of stack can grow upto the size of the input string which is n, so O(n) auxiliary space will be used.
Conclusion
- One can solve this balanced parentheses problem using stack.
- There is some sort of nested symbol in almost every type of notation that needs to match in a balanced order.
- O(n) time complexity and O(n) auxiliary space is consumed in this stack approach.




