The following are some code to check if the given expression is balanced parenthesis or not
Balanced parentheses: "[()]{}{()()}"
UnBalanced parentheses: "[(])"
- Declare a character stack S.
- Now traverse the expression string exp.
- If the current character is a starting bracket ( ‘(‘ or ‘{‘ or ‘[‘ )then push it to stack.
- If the current character is a closing bracket ( ‘)’ or ‘}’ or ‘]’ ) then pop from stack and if the popped character is the matching starting bracket then fine else parenthesis are not balanced.
- After complete traversal, if there is some starting bracket left in stack then “not balanced”
- Python
- C
- JAVA
- For Python -
python parent_checker.py
- For C - Compile the file using
gcc parent_checker.c
After that create an executable file usinggcc -o parent_checker parent_checker.c
After creating executable file, execute the file usingparent_checker.exe
- For JAVA - Compile the file using
javac Parent_checker.java
After that run the file usingjava Parent_checker
Codes in other lanuage like C++, Javascript etc...