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/
Your article is one of its kind which explained every bit of Custom Build Website. looking for further valuable articles from you
ReplyDeleteYou've written a pretty interesting article. I was on the lookout for information like this. Please share more related information with me so that I can learn more. Best Custom Websites
ReplyDeleteGreat content. I was looking for this kind of content. It saved my time to search further.
ReplyDeleteYou must provide such type of content constantly. I appreciate your willingness to provide readers with a good article.
Custom Design Websites
Thank you Sir, you made it easy as pie. Now i am able to understand and have enough knowledge about this. It is only because of you.
ReplyDeleteCustom Designed Websites
Thanks to your article now I understand or learn many new things which are very difficult to understand the way you describe this topic is very easy to understand. Web Design USA
ReplyDeletehttps://danilovdev.blogspot.com/2018/05/balanced-parentheses-in-swift.html
ReplyDeleteThank you very much for taking the time to share this important
ReplyDeleteinformation with us. This is incredible.
SEO Company NYC
pusulabet
ReplyDeletesex hattı
https://izmirkizlari.com
rulet siteleri
rexbet
L6K7