valid parentheses

Ravi Kumar 11 months ago
parent e423006675 commit e24f0dd6b785c3fb736e30e80f3f51a7f83fc781
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