-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_string.py
More file actions
33 lines (25 loc) · 788 Bytes
/
Copy pathcheck_string.py
File metadata and controls
33 lines (25 loc) · 788 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# check if a string containing parenthesis() ,brackets[] ,braces{} is valid
def isValid(s):
stack = []
for ch in s:
if ch == '{' or ch == '[' or ch == '(' :
stack.append(ch)
elif ch == '}':
if not stack or stack[-1] != '{':
return False
stack.pop()
elif ch == ']':
if not stack or stack[-1] != '[':
return False
stack.pop()
elif ch == ')':
if not stack or stack[-1] != '(':
return False
stack.pop()
return len(stack) == 0
print(isValid("{([])}"))
print(isValid("{(])}"))
print(isValid("([])}"))
print(isValid("()"))
print(isValid("{"))
print(isValid(""))