Question: Please help with JAVA assignment You are to create a program that parses a string with brackets. The program must be able to check whether

Please help with JAVA assignment

You are to create a program that parses a string with brackets. The program must be able to check whether the brackets: {}, [], and (), are balanced. If all the brackets in the string are correctly balanced, then the program must output Success. Otherwise, output the 1-based index of the first unmatched closing bracket. If there are no closing brackets, then output the first unmatched opening bracket.

Input: A string S of lowercase alphabets (e.g., a, b, c,,z), digits, punctuation marks, and the brackets: [],(), and {}.

Output: Success (excluding the quotation marks) or the 1-based index of the first unmatched closing bracket. If there are no closing brackets, then output the 1-based index of the first unmatched opening bracket.

Hint: Notice that when reading an opening bracket we expect a corresponding closing bracket somewhere in the string. What data structure best stores the closing brackets so we can get them later in the correct order?

Sample cases:

===================================

Example # 1:

Input: print(hello world!)

Output: Success

----------------------------------------------------------

Explanation: All the brackets are matched.

===================================

Example # 2:

Input: foo(bar[i);

Output: 10

---------------------------------------------------------

Explanation: ) doesnt match with [, so ) is the first unmatched closing bracket.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!