-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidParentheses.py
34 lines (29 loc) · 959 Bytes
/
ValidParentheses.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
'''
Description:
Write a function called that takes a string of parentheses, and determines if the order of the parentheses is valid.
The function should return true if the string is valid, and false if it's invalid.
Examples
"()" => true
")(()))" => false
"(" => false
"(())((()())())" => true
Constraints
0 <= input.length <= 100
Along with opening (() and closing ()) parenthesis, input may contain any valid ASCII characters.
Furthermore, the input string may be empty and/or not contain any parentheses at all.
Do not treat other forms of brackets as parentheses (e.g. [], {}, <>).
'''
def valid_parentheses(string):
a = 0
for i in string:
if a < 0:
return False
if i == '(':
a +=1
elif i == ')':
a -= 1
if a != 0:
return False
else:
return True
print(valid_parentheses('hi())('))