Skip to content

Commit c8b9333

Browse files
authored
Minimum Bracket Reversal
Initial File
1 parent 05b8871 commit c8b9333

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Minimum Bracket Reversal

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def min_bracket_reversal(exp):
2+
size=len(exp)
3+
if size%2!=0:
4+
return None
5+
s=[]
6+
for i in range(size):
7+
if exp[i]=='}' and len(s)>0:
8+
if s[0] == '{':
9+
s.pop(0)
10+
else:
11+
s.insert(0,exp[i])
12+
else:
13+
s.insert(0,exp[i])
14+
15+
total_left = len(s)
16+
open=0
17+
while( len(s)>0 and s[0] == '{'):
18+
s.pop(0)
19+
open=open+1
20+
return total_left//2 + open % 2
21+
22+
expr = "{{}{{}{{"
23+
print(min_bracket_reversal(expr))

0 commit comments

Comments
 (0)