Balanced Brackets
Is the bracket string balanced? Every ( must be closed by a later ), and no prefix may close more than it has opened.
Input: one line with a string of ( and ).
Output: YES if balanced, otherwise NO.
Constraints:
- 1 ≤ |S| ≤ 105
Sample Tests
Input 1
(()())
Output 1
YES
Input 2
())(
Output 2
NO
Discussion (opens after you solve — no spoilers before)
No comments yet.