#K60057. Valid Parentheses Checker
Valid Parentheses Checker
Valid Parentheses Checker
Given a string consisting solely of the characters '(', ')', '{', '}', '[' and ']', your task is to determine if the input string is a valid sequence of parentheses.
A sequence is considered valid if:
- Every opening bracket has a corresponding closing bracket of the same type.
- The brackets are closed in the correct order.
An empty string is also considered a valid sequence.
The validity condition can be mathematically expressed using a stack data structure. In particular, for any input string \( S \), the sequence is valid if and only if after processing all characters with the following rule, the stack is empty:
For each character \( c \) in \( S \):
- If \( c \) is an opening bracket, push it onto the stack.
- If \( c \) is a closing bracket, check if the top of the stack is the matching opening bracket. If it is, pop the top element; otherwise, the sequence is invalid.
inputFormat
The input consists of a single line containing a string of characters. The string will only include the characters '(', ')', '{', '}', '[' and ']'.
outputFormat
Output a single line: YES
if the parentheses sequence is valid, and NO
otherwise.
()
YES