Problem: Find the place where the bracket doesn’t ends properly or the chain breaks.
Input Format: First line contains a single integer, the number of testcases n. Next n line contains a string consists of brackets.
(NOTE: The actual problem didn’t have the number of testcases as input. But for your better convenience we have worked with testcases.)
Sample Input:
3
())
[][][
{{[]}}}
Sample Output:
3
6
7
Explanation: In the first input “())” third index the bracket closes but it doesn’t have any opening bracket (the chain breaks), the first bracket in position 1 is closed in the second position, Hence the output should be 3.
In the second example “[][][“, bracket in the 5th position opens but doesn’t close anywhere. Hence it returned the position where it should have closed (position of chain break) i.e. 6.
In the last example, the closing bracket in the 7th position is extra. It closes but doesn’t have any opening bracket of it (breaking of the chain). Hence it returned 7.
Solution: We strongly recommend you to try the problem first before moving to the solution.
# Solution of Bracket Validator problem in Python
# www.codewindow.in
def validator(str): # Function validator to check
st=[]
count=0
for a in str:
if a=='[' or a=='{' or a=='(':
st.append(a)
count+=1
continue
if len(st)==0:
return count+1
x=st.pop()
if a==']' and x=='[':
count+=1
elif a=='}' and x=='{':
count+=1
elif a==')' and x=='(':
count+=1
else:
return count+1
if len(st)==0:
return 0
else:
return count+1
# Driver Code
test_case=int(input()); # Take number of testcases
while(test_case):
str=input()
print(validator(str))
test_case-=1
Input:
3
())
[][][
{{[]}}}
Output:
3
6
7
Follow Us
You Missed
- Unlocking Innovation and Diversity: Accenture HackDiva Empowers Women in Tech with Cutting-Edge Solutions – codewindow.in
- QA Engineer Opportunities at Siemens Company: Apply Now – codewindow.in
- QA Engineer Opportunities at Siemens Company: Apply Now – codewindow.in
- Software Engineer Positions at Siemens Company: Apply Now – codewindow.in
- Cloud Engineer II Opportunities at Insight Company: Apply Now – codewindow.in
- Shape Your Career: Assistant Engineer Opportunities at Jindal Company – codewindow.in
- Shape Your Future: Executive Opportunities at Jindal Company – cdewindow.in
- Associate Engineer, Software Development at Ingram: Apply Now – codewindow.in
- Jade Company’s UI/UX Development Engineer Opportunities – Apply Now – codewindow.in
- Transform Your Career with S&P Global: Apply for the Software Development Engineer Role and Lead the Future of Financial Technology Innovation – codewindow.in
- Unlock Your Potential at Accenture as an Associate Software Engineer – Elevate Your Career with Innovation and Excellence – codewindow.in
- Accelerate Your Career: Join NVIDIA’s Elite Software Engineering Internship Program and Shape the Future of Technology – codewindow.in
- C Programming Interview Questions – codewindow.in
- Lead the Way in Analytics: Specialist Position at Razorpay – codewindow.in
- Innovate with Cyient: Junior Software Engineer Wanted – codewindow.in
- Innovate with Volvo: Associate Software Engineer Wanted – codewindow.in
- Lead the Tech Revolution: Full Stack Developer at Unisys – codewindow.in
- Software Engineer at ABB: Unlock Innovation and Shape the Future – codewindow.in
- IBM Associate Systems Engineer Job: Boost Your Career with a Leading Technology Giant – codewindow.in
- Make Your Mark in Android Development: Join Concentrix – codewindow.in
- Infosys is Growing: Field Services Developer Role Now Open – codewindow.in
- Start Your IT Career Journey with Amazon: IT Services Support Associate I Opportunity – codewindow.in
- Shape the Future of Web: Front-End Software Engineer Opportunity at Google Cloud – codewindow.in
- Barclays QA Team Expands: QA Analyst Role Now Open- codewindow.in
- Eurofins QA Team Grows: Test Engineer Role Now Open – codewindow.in
- Exciting Opportunity: Java Spring Boot Senior Developer Role at Infosys – codewindow.in
- Unlock Your Potential at Nokia: Software Engineer Opportunities Await – codewindow.in
- Join Microsoft’s World-Class Team as a Software Engineer and Shape the Future of Technology – codewindow.in
- Virtusa is Seeking Talented React JS Developers to Drive Digital Excellence – codewindow.in
- Join IBM Dynamic Team as a Full Stack Developer and Shape the Future – codewindow.in
- EY Welcomes Aspiring AI/ML Interns to Unlock the Future of -codewindow.in
- Exciting Opportunity: Project Engineer at Rockwell Automation- codewindow.in