Question: http://oj.leetcode.com/problems/valid-parentheses/
Question Name: Valid Parentheses
Solution to Valid Parentheses by LeetCode
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Solution: # @return a boolean def isValid(self, s): stack = [] matchingTable = {"(":")", "[":"]", "{":"}"} for letter in s: if letter == "(" or letter == "[" or letter == "{": stack.append(letter) elif letter == ")" or letter == "]" or letter == "}": if len(stack) == 0 or letter != matchingTable[stack.pop()]: # Unmatched parentheses found return False else: # Only valid parentheses. pass return len(stack) == 0 |