#K12046. Valid Parentheses Sequence
Valid Parentheses Sequence
Valid Parentheses Sequence
You are given a string s
consisting solely of the characters (
and )
. Your task is to determine whether the string is a valid parentheses sequence.
A parentheses sequence is called valid if it satisfies the following conditions:
- For every prefix of the string, the number of opening parentheses is greater than or equal to the number of closing parentheses.
- The total number of opening and closing parentheses is equal, i.e. \( \text{count}(\text{'('}) = \text{count}(\text{')'}) \).
If the sequence is valid, output YES
; otherwise, output NO
.
inputFormat
The input consists of a single line containing a string s
with only the characters (
and )
. The string may be empty.
outputFormat
Output a single line: YES
if the given string is a valid parentheses sequence, and NO
otherwise.
()
YES