Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- myfile=open('text.txt')
- i = 0
- rep = 0
- open = 0
- ans = 1e9
- first_close = 1e9
- tot_close = 0
- tot_open = 0
- while(myfile):
- a = myfile.get()
- if (a!="{" and a!="}"):
- i+=1
- continue
- if (a=='{'):
- open+=1
- tot_open+=1
- else:
- first_close=min(first_close, i+1)
- if(open==0):
- rep+=1
- else:
- close+=1
- tot_close+=1
- if(open==1):
- ans=min(ans,i+1)
- if(open==0):
- ans=1e9
- i+=1
- if(rep>1 or abs(tot_open-tot_close)>1):
- print(-1)
- elif(tot_open-tot_close == 0 or (open>1) or (rep==1 and open>0)):
- print(-1)
- else:
- if(tot_close>tot_open):
- print(first_close)
- else:
- print(ans)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement