Problems > Balanced Brackets

Balanced Brackets

Difficulty: 1000 2000 ms 128 MB

implementation strings

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

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.