Problem
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
Algorithm
Maintain a stack, and push all left parentheses into the stack. Whenever see a right parentheses, peek the top element of the stack. If they does not match, return false. Finally, if the stack is not empty, return false. Remember to check empty stack before peek.
No comments:
Post a Comment