Valid Parentheses @Leetcode -- Python
http://oj.leetcode.com/problems/valid-parentheses/
1 class Solution: 2 # @return a boolean 3 def isValid(self, s): 4 if s == '': 5 return True 6 left = '([{' 7 right = ')]}' 8 stack = [] 9 for i in s: 10 if i == '(' or i == '[' or i == '{': 11 stack.append(i) 12 continue 13 for j in xrange(3): 14 if i == right[j]: 15 if not stack or stack[-1] != left[j]: 16 return False 17 else: 18 stack.pop() 19 continue 20 return not stack 21 # test 22 s = Solution() 23 print s.isValid('()')