-
Notifications
You must be signed in to change notification settings - Fork 0
/
valid_parentheses.py
48 lines (42 loc) · 1.29 KB
/
valid_parentheses.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'''
valid_parentheses("()") # True
valid_parentheses(")(()))") # False
valid_parentheses("(") # False
valid_parentheses("(())((()())())") # True
valid_parentheses('))((') # False
valid_parentheses('())(') # False
valid_parentheses('()()()()())()(') # False
'''
#
#
# def valid_parentheses(paren_string):
# result = 0
# for p in list(paren_string):
# if result < 0:
# return False
# elif p == "(":
# result += 1
# elif p == ")":
# result -= 1
# return result == 0
#
#
# print(valid_parentheses("()")) # True
# print(valid_parentheses(")(()))")) # False
# print(valid_parentheses("(")) # False
# print(valid_parentheses("(())((()())())")) # True
# print(valid_parentheses('))((')) # False
# print(valid_parentheses('())(')) # False
# print(valid_parentheses('()()()()())()(')) # False
def valid_parentheses(string):
check = string
while "()" in check:
check = "".join(check.split("()"))
return len(check) == 0
print(valid_parentheses("()")) # True
print(valid_parentheses(")(()))")) # False
print(valid_parentheses("(")) # False
print(valid_parentheses("(())((()())())")) # True
print(valid_parentheses('))((')) # False
print(valid_parentheses('())(')) # False
print(valid_parentheses('()()()()())(s)(')) # False