-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrackets.py
More file actions
executable file
·40 lines (35 loc) · 996 Bytes
/
brackets.py
File metadata and controls
executable file
·40 lines (35 loc) · 996 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
34
35
36
37
38
39
40
def solutions(S):
"""Check that a string is properly nested.
A string is considered properly nested if one of the following is true:
(1) S is empty
(2) S has the form "(U)" or "[U]" or "{U}" where U is properly nested
(3) S has the form "VW" where V and W are properly nested
Args:
S (str): An N character long string.
Returns:
int: 1 if S is properly nested, 0 if it is not.
Complexity:
Time: O(N)
Space: O(N)
>>> solutions('')
1
>>> solutions('{{}}')
1
>>> solutions('{{)}')
0
>>> solutions('{{}()}')
1
>>> solutions('{{}())')
0
"""
stack = []
brackets = {'{': '}', '[': ']', '(': ')'}
for x in S:
if x in brackets:
stack.append(brackets[x])
elif x in brackets.values():
if stack.pop() == x:
continue
else:
return 0
return 1