-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathdelim_balanced.py
64 lines (53 loc) · 2.37 KB
/
delim_balanced.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'''
This problem is taken from
Stanford's CS 9: Problem-Solving for the CS Technical Interview.
Write a function that takes as input a string and returns
whether the parenthesis are "balanced".
If the candidate solves this question quickly, add the following difficulty:
the string may also contain "[]" or "{}". Return whether all three types of
brackets are balanced.
Example
"(())", "(()())", or "()(()())" should return true
"(()" or "())" should return false
My solution:
You can pass in a list of opening delimiters with
a list of their corresponding closing delimiters.
Returns True if all delims are balanced, False otherwise.
The idea here is that everytime you encounter an opening delim of a
certain type, increase the count for that delim by 1.
When you encouter a closing delim:
+ decrease the count by 1
+ if the count now is negative, it's not balanced.
'''
def is_balanced(string, delims=[], closes=[]):
offsets = [0 for _ in delims]
delims_map = {delims[i]: i for i in range(len(delims))}
closes_map = {closes[i]: i for i in range(len(closes))}
for char in string:
if char in delims_map:
offsets[delims_map[char]] += 1
elif char in closes_map:
offsets[closes_map[char]] -= 1
if offsets[closes_map[char]] < 0:
return False
for i in offsets:
if i != 0:
return False
return True
assert not is_balanced('(()', delims=['('], closes=[')'])
assert is_balanced('(())', delims=['('], closes=[')'])
assert is_balanced('()(()())', delims=['('], closes=[')'])
assert is_balanced('()(()())()', delims=['('], closes=[')'])
assert is_balanced('(()())', delims=['('], closes=[')'])
assert not is_balanced('(()))', delims=['('], closes=[')'])
assert not is_balanced('(()))', delims=['(', '{'], closes=[')', '}'])
assert not is_balanced('(())){', delims=['(', '{'], closes=[')', '}'])
assert not is_balanced('(({)))}', delims=['(', '{'], closes=[')', '}'])
assert is_balanced('(({))()}', delims=['(', '{'], closes=[')', '}'])
assert not is_balanced('[(', delims=['(', '{', '['], closes=[')', '}', ']'])
assert is_balanced('()({(})([]))[()]',
delims=['(', '{', '['],
closes=[')', '}', ']'])
assert not is_balanced('()({(}{)([]))[()]',
delims=['(', '{', '['],
closes=[')', '}', ']'])