Validate a Bracket Sequence
Problem statement
Given a string sequence containing only the bracket characters ()[]{}, return whether it is valid.
A valid sequence closes every opening bracket with the same bracket type, and closes brackets in the reverse order in which they were opened.
Function
isValid(sequence: String) → booleanExamples
Example 1
sequence = "([]{})"return = trueEvery closing bracket matches the most recent unmatched opening bracket.
Example 2
sequence = "([)]"return = falseThe closing parenthesis appears before the nested square bracket closes.
Constraints
0 <= sequence.length <= 100000.- Every character is one of
(,),[,],{, or}.
Source note: The source image is a source-neutral excerpt of the author-reported prompt.