Valid Parenthesis
Question (LC.20)
Example
Input: ()[]{}
Output: true
Input: ([)]
Output: falseAnalysis
Approach
iterate through the input string
push open brackets onto a stack
pop from stack when closed brackets are detected
if the stack is empty, return false (more closed than open)
if the brackets don't match (wrong type), also return false
return if the the stack is empty (all open and closed match)Code
Reflection
Last updated