#C366. Valid Parentheses, Brackets, and Braces
Valid Parentheses, Brackets, and Braces
Valid Parentheses, Brackets, and Braces
Given a string consisting solely of the characters '(', ')', '{', '}', '[' and ']', determine whether the string forms a valid sequence of brackets. A string is considered valid if:
1. Every opening bracket has a corresponding closing bracket.
2. Brackets are closed in the correct order.
Formally, a string ( S ) is valid if, when scanned from left to right, every closing bracket ( c ) has its corresponding opening bracket ( matching(c) ) at the top of the stack, i.e., ( matching(')') = '(' ), ( matching(']') = '[' ), and ( matching('}') = '{' ), and the stack is empty at the end of the scan.
inputFormat
Input is given as a single line from standard input. The line contains a string composed only of the characters '(', ')', '{', '}', '[' and ']'. The input may be empty.
outputFormat
Print a single line to standard output containing "YES" if the string is a valid sequence; otherwise, print "NO".## sample
([])
YES
</p>