-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathMinimumSwapsForBracketBalancing.py
60 lines (49 loc) · 1.64 KB
/
MinimumSwapsForBracketBalancing.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
"""
@author Anirudh Sharma
You are given a string S of 2N characters consisting of N ‘[‘ brackets and N ‘]’ brackets.
A string is considered balanced if it can be represented in the for S2[S1] where S1 and S2
are balanced strings.
We can make an unbalanced string balanced by swapping adjacent characters.
Calculate the minimum number of swaps necessary to make a string balanced.
Note - Strings S1 and S2 can be empty.
"""
def minimumNumberOfSwaps(S):
# Number of swaps required
swaps = 0
# Special case
if S is None or len(S) == 0:
return swaps
# Variable to track open brackets
openBrackets = 0
# Loop through the string
for c in S:
# If we encounter the left bracket,
# we will increment the count
if c == '[':
openBrackets += 1
# If we encounter the right bracket,
# then any of the two conditions can
# happen
else:
# If there are open brackets to the
# left of the current bracket,
# close the last encountered open
# bracket
if openBrackets != 0:
openBrackets -= 1
# If not, we will have to perform
# swap
else:
swaps += 1
# Reset the count of open brackets
openBrackets = 1
# We will need n/2 inversions for extra open brackets
# to make the string balanced
return swaps + openBrackets // 2
if __name__ == "__main__":
S = "[]][]["
print(minimumNumberOfSwaps(S))
S = "[[][]]"
print(minimumNumberOfSwaps(S))
S = "][][]["
print(minimumNumberOfSwaps(S))