-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbracket_match.py
38 lines (29 loc) · 1.13 KB
/
bracket_match.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
"""
Bracket Match
A string of brackets is considered correctly matched if every opening bracket in the string can be paired up with a later closing bracket, and vice versa.
For instance, “(())()” is correctly matched, whereas “)(“ and “((” aren’t. For instance, “((” could become correctly matched by adding two closing brackets
at the end, so you’d return 2.
Given a string that consists of brackets, write a function bracketMatch that takes a bracket string as an input and returns the minimum number of brackets
you’d need to add to the input in order to make it correctly matched.
input: text = “(()”
output: 1
input: text = “(())”
output: 0
input: text = “())(”
output: 2
"""
# O(n) time
# O(1) space
def bracket_match(text):
count_missing_closing = 0
count_missing_opening = 0
n = len(text)
for char in text:
if char == "(":
count_missing_closing += 1
elif char == ")":
count_missing_closing -= 1
if count_missing_closing < 0:
count_missing_closing += 1
count_missing_opening += 1
return count_missing_closing + count_missing_opening