valid parentheses

Priya Raman 1 year ago
parent 1ad7c0b747 commit 3936bf8c5cf646f0b78150cc96000a6d393ade5e
1 changed files with 9 additions and 0 deletions
+9-0valid_parentheses.py ADDED
View File
@@ -0,0 +1,9 @@
1+def is_valid(s):
2+ pairs = {")": "(", "]": "[", "}": "{"}
3+ stack = []
4+ for ch in s:
5+ if ch in pairs.values():
6+ stack.append(ch)
7+ elif not stack or stack.pop() != pairs[ch]:
8+ return False
9+ return not stack