We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3417705 commit 67ac26bCopy full SHA for 67ac26b
alternative/easy/valid_parentheses.py
@@ -0,0 +1,19 @@
1
+def is_valid(s):
2
+ stack = []
3
+ for c in s:
4
+ if c in "([{":
5
+ stack.append(c)
6
+ elif c == ')' and (not stack or stack.pop() != '('):
7
+ return False
8
+ elif c == ']' and (not stack or stack.pop() != '['):
9
10
+ elif c == '}' and (not stack or stack.pop() != '{'):
11
12
+ return not stack
13
+
14
+# Test cases
15
+assert is_valid("()") == True
16
+assert is_valid("()[]{}") == True
17
+assert is_valid("(]") == False
18
+assert is_valid("([)]") == False
19
+assert is_valid("{[]}") == True
0 commit comments