-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalid_parentheses.py
More file actions
28 lines (24 loc) · 876 Bytes
/
valid_parentheses.py
File metadata and controls
28 lines (24 loc) · 876 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
"""
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):
value = 0
for char in string:
if char == '(':
value += 1
elif char == ')':
value -= 1
if value < 0:
return False
return True if value == 0 else False