#C9936. Valid Parentheses Checker
Valid Parentheses Checker
Valid Parentheses Checker
This problem requires you to determine whether a given string containing only parentheses is valid. A valid string follows these rules:
- Every opening parenthesis
(
must have a corresponding closing parenthesis)
. - Parentheses must be properly nested. For example, (()) is valid but (() and (())) are not.
The task is to read the input string from standard input and output either True
if the string is valid or False
if it is not. The solution should correctly implement the concept of a stack to ensure that the parentheses are balanced.
You may use the following mathematical formulation for balanced parentheses: Use a stack, and for each character in the string, push for (
and pop for )
. The string is valid if and only if the stack is empty at the end and no pop occurred on an empty stack.
inputFormat
The input consists of a single line containing a string s
that comprises only the characters '(' and ')'.
outputFormat
Output True
if the string s
is a valid sequence of parentheses, otherwise output False
.
(())
True