#C14920. Valid Parentheses Checker
Valid Parentheses Checker
Valid Parentheses Checker
Given a string consisting only of the characters '(' and ')', determine whether the string forms a valid sequence of parentheses.
A sequence is considered valid if and only if:
- Every opening parenthesis '(' has a corresponding closing parenthesis ')'.
- Parentheses must be closed in the correct order.
In other words, for a string s to be valid, the following conditions must hold:
\(\text{For every prefix of } s, \; \#( \geq \#)\)
and
\(\text{Total number of } '(' \text{ equals the total number of } ')'\).
For example, ()
and (())
are valid, while (())))
and ((())
are invalid.
inputFormat
The input consists of a single line containing a string s composed solely of the characters '(' and ')'.
outputFormat
Output a single line: True
if the string is a valid sequence of parentheses, or False
otherwise.
()
True