class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack = []
for c in s:
if c == '(' or c == '[' or c == '{':
stack.append(c)
if c == ')':
if stack and stack[1] == '(':
stack.pop(1)
continue
else:
return False
if c == ']':
if stack and stack[1] == '[':
stack.pop(1)
continue
else:
return False
if c == '}':
if stack and stack[1] == '{':
stack.pop(1)
continue
else:
return False
return not stack
runtime beats 23.91% of python submissions


@CGQAQ said in runtime beats 23.91% of python submissions:
class Solution(object):

def isValid(self, s): """ :type s: str :rtype: bool """ stack = [] for c in s: if c == '(' or c == '[' or c == '{': stack.append(c) if c == ')': if stack and stack[1] == '(': stack.pop(1) continue else: return False if c == ']': if stack and stack[1] == '[': stack.pop(1) continue else: return False if c == '}': if stack and stack[1] == '{': stack.pop(1) continue else: return False return not stack
