You are here : InfyTQ Archive >> InfyTQ Coding Problems
You are given a string of brackets (, ), {, }, [, ], your job is to find the position of string where order of brackets breaks.
Input :
First Input : String of Brackets
Output :
Position where order breaks. (keep 1-based indexing in mind)
Sample Testcases :
I/P 1:
[(((())))][
O/P 1:
12
I/P 2:
{{]}()
O/P 2:
3
Solution :
s1=[]
op=[‘(‘,'{‘,'[‘]
cl=[‘)’,’}’,’]’]
def validate(s):
for i in range(len(s)):
if s[i] in op:
s1.append(s[i])
elif s[i] in cl:
res=cl.index(s[i])
if(len(s1)>0) and (op[res]==s1[len(s1)-1]):
s1.pop()
else:
return (i+1)
if(len(s1)==0):
return 0
else:
return len(s)+1
s=str(input())
print(validate(s))