MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / isValid

Method isValid

python/0020-valid-parentheses.py:2–14  ·  view source on GitHub ↗
(self, s: str)

Source from the content-addressed store, hash-verified

1class Solution:
2 def isValid(self, s: str) -> bool:
3 bracketMap = {")": "(", "]": "[", "}": "{"}
4 stack = []
5
6 for c in s:
7 if c not in bracketMap:
8 stack.append(c)
9 continue
10 if not stack or stack[-1] != bracketMap[c]:
11 return False
12 stack.pop()
13
14 return not stack

Callers

nothing calls this directly

Calls 1

popMethod · 0.45

Tested by

no test coverage detected