Given an expression, find and mark matched and unmatched parenthesis in it. We need to replace all balanced opening parenthesis with 0, balanced closing parenthesis with 1 and all unbalanced with -1.
Input : ((a) Output : -10a1 Input : (a)) Output : 0a1-1 Input : (((abc))((d))))) Output : 000abc1100d111-1-1
The idea is based on stack. We run a loop from the start of the string upto end and for every ‘(‘, we push it into stack. If stack is empty and we encounter an closing bracket ‘)’ the we replace -1 at that index of the strig. Else we replace all opening brackets ‘(‘ with 0 and closing brackets with 1. Then pop from the stack.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find if an expression has duplicate parenthesis or not
- Check for balanced parenthesis without using stack
- Number of balanced parenthesis substrings
- Reverse substrings between each pair of parenthesis
- Count all indices of cyclic regular parenthesis
- Calculate weight of parenthesis based on the given conditions
- Find maximum depth of nested parenthesis in a string
- InfyTQ 2019 : Find the position from where the parenthesis is not balanced
- Expression Evaluation
- Balanced expression with replacement
- Expression contains redundant bracket or not
- Arithmetic Expression Evalution
- Check for balanced parentheses in an expression
- Check if an URL is valid or not using Regular Expression
- How to validate a domain name using Regular Expression
- Check if expression contains redundant bracket or not | Set 2
- Parsing String of symbols to Expression
- How to validate HTML tag using Regular Expression
- Check if given Parentheses expression is balanced or not
- Evaluate an array expression with numbers, + and -
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.