151. Balanced Parentheses
Core1000 ms256 MBSolved by 0%
Read one line containing only ( and ) and print Yes if the brackets
are balanced, or No otherwise.
Balanced means every opening bracket has a matching closing one, in the right order. So
(()) is balanced and )( is not.
An empty line counts as balanced.
Constraints - The line has between 0 and 100000 characters. - Only `(` and `)` appear.
Input
A single line containing only ( and ). It may be empty.
Output
Print exactly one word: Yes or No. An empty line is balanced.
Input(())
OutputYes
Noteis properly nested
Input(()
OutputNo
Noteleaves one bracket open at the end, which a counter reaching zero would miss
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.
Hint 1Where to start
Hint 2The approachOpen hint 1 first. This one carries on from it.
Hint 3PseudocodeOpen hint 2 first. This one carries on from it.
Hint 4Full solutionOpen hint 3 first. This one carries on from it.