MCPcopy Create free account
hub / github.com/HuberTRoy/leetCode / isValid

Method isValid

String/ValidParentheses.py:41–65  ·  view source on GitHub ↗

:type s: str :rtype: bool

(self, s)

Source from the content-addressed store, hash-verified

39
40class Solution(object):
41 def isValid(self, s):
42 """
43 :type s: str
44 :rtype: bool
45 """
46 left = "({["
47 left_key = {')': '(', ']': '[', '}': '{'}
48
49 stack = []
50
51 for i in s:
52 if i in left:
53 stack.append(i)
54 else:
55 try:
56 if stack[-1] == left_key[i]:
57 stack.pop()
58 else:
59 return False
60 except:
61 return False
62
63 if stack:
64 return False
65 return True

Callers

nothing calls this directly

Calls 1

popMethod · 0.45

Tested by

no test coverage detected