Following task is given: Define that sequence of parenthesis is balanced (correct). For example,
Algorithm
This algorithm will work for simple cases when close parenthesis appears after open.
For example, it will work correct for "((( )))".
But in case when close parenthesis appears before open this algorithm will be wrong and return true even if parentheses are not balanced. So, for fixing this mistake we need to check that current balance is equal to zero before decrease operation (Zero balance means that next must be open parenthesis but not close. And if next parenthesis is close and current balance is equal to zero then we have NOT balanced parentheses).
In case of when next character is close parenthesis we need to check current balance and return false if balance is equal to zero.
In this case we need more advanced algorithm for resolving this task, because simple counter cannot handle this. Here we can solve this using Stack Data Structure. Idea is following: add to stack every next OPEN parenthesis and get from stack when next parenthesis is CLOSE and compare that they are valid pair.
I already wrote about Stack Data Structure
Stack Implementation
Algorithm
Source code can be found on GitHub: Balanced Parentheses in Swift
The list of useful links
- ((( ))) is balanced
- (( ))) is NOT balanced
- ) (( )) is NOT balanced
- (( )) () is balanced
Simple Case (One type of parenthesis)
Algorithm
- For any open parenthesis found in string we will increment counter by 1
- For any close parenthesis found in string we will decrease counter by 1
- If after all the characters in the string are checked, the counter is equal to zero then it is balanced parentheses sequence.
This algorithm will work for simple cases when close parenthesis appears after open.
For example, it will work correct for "((( )))".
But in case when close parenthesis appears before open this algorithm will be wrong and return true even if parentheses are not balanced. So, for fixing this mistake we need to check that current balance is equal to zero before decrease operation (Zero balance means that next must be open parenthesis but not close. And if next parenthesis is close and current balance is equal to zero then we have NOT balanced parentheses).
Simple Case with Checking
In case of when next character is close parenthesis we need to check current balance and return false if balance is equal to zero.
Case with Multiple Types of Parentheses (Using Stack)
But what if there are more then one type of parenthesis, for example, three: {}, [], ().In this case we need more advanced algorithm for resolving this task, because simple counter cannot handle this. Here we can solve this using Stack Data Structure. Idea is following: add to stack every next OPEN parenthesis and get from stack when next parenthesis is CLOSE and compare that they are valid pair.
I already wrote about Stack Data Structure
Stack Implementation
Algorithm
- For each open parenthesis we add it to stack
- For each close parenthesis we do following:
- If stack is empty then return false (NOT balanced parentheses)
- Pop value from stack and compare with this close parenthesis, if they NOT equal then return false (NOT balanced parentheses)
- After loop if finished we check that stack is empty, if it is empty then we have balanced parentheses
Source Code
Source code can be found on GitHub: Balanced Parentheses in Swift
References
The list of useful links
- http://interactivepython.org/courselib/static/pythonds/BasicDS/SimpleBalancedParentheses.html
- https://stackoverflow.com/questions/18482654/to-check-if-parenthesis-are-balanced-without-stack
- http://blog.cybdev.org/pravilnaya-skobochnaya-posledovatelnost-ili-zadacha-o-skobkah
- https://stackoverflow.com/questions/23187539/java-balanced-expressions-check
- https://www.youtube.com/watch?v=IhJGJG-9Dx8
- https://www.youtube.com/watch?v=1kseKf5HAaM
- http://www.geeksforgeeks.org/check-for-balanced-parentheses-in-an-expression/