Check for balanced parentheses in an expression | GeeksforGeeks Given an expression string exp, write...

Please Visit: http://ift.tt/1ajReyV



Check for balanced parentheses in an expression | GeeksforGeeks

Given an expression string exp, write a program to examine whether the pairs and the orders of “{“,”}”,”(“,”)”,”[","]” are correct in exp. For example, the program should print true for exp = “[()]{}{[()()]()}” and false for exp = “[(])”

Algorithm:

1) Declare a character stack S.

2) Now traverse the expression string exp.

a) If the current character is a starting bracket (‘(‘ or ‘{‘ or ‘[') then push it to stack.

b) 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.

3) After complete traversal, if there is some starting bracket left in stack then “not balanced”

http://ift.tt/UtF8CX

http://ift.tt/1lYaXte



Check for balanced parentheses in an expression | GeeksforGeeks







from Public RSS-Feed of Jeffery yuan. Created with the PIXELMECHANICS 'GPlusRSS-Webtool' at http://gplusrss.com http://ift.tt/UtF8D1

via LifeLong Community

No comments:

Post a Comment