Balanced Paranthesis
For a given a string expression containing only round brackets or parentheses, check if they are balanced or not. Brackets are said to be balanced if the bracket which opens last, closes first.
For a given a string expression containing only round brackets or parentheses, check if they are balanced or not. Brackets are said to be balanced if the bracket which opens last, closes first.
Expression: (()())
Since all the opening brackets have their corresponding closing brackets, we say it is balanced and hence the output will be, 'true'.
The input expression will not contain spaces in between.
The first and the only line of input contains a string expression without any spaces in between.
The only line of output prints 'true' or 'false'.
You don't have to print anything explicitly. It has been taken care of. Just implement the function.
1 <= N <= 10^7
Where N is the length of the expression.
Time Limit: 1sec
(()()())
true
()()(()
false
The initial two pairs of brackets are balanced. But when you see, the opening bracket at the fourth index doesn't have its corresponding closing bracket which makes it imbalanced and in turn, making the whole expression imbalanced. Hence the output prints 'false'.
Comments
Post a Comment
Please give us your valuable feedback