Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please write the code in C language Exercise 2.29 Making Parentheses String Balanced Given a string S that only consists of open parentheses ( and
Please write the code in C language
Exercise 2.29 Making Parentheses String Balanced Given a string S that only consists of open parentheses "(" and close parentheses ")", you are asked to determine the minimum number of additional parentheses needed to make the parentheses string S balanced. Note that the additional parentheses can be added to any position of string S, and can be either open parentheses "(" or close parentheses ")". Formally speaking, a parentheses string S is balanced if and only if - S is an empty string, or - S=AB, where A and B are both balanced parentheses strings, or - S=(A), where A is a balanced parentheses string. Input The first line is an integer N(1N103), indicating the number of test cases. Each test case contains two lines. The first line is an integer m(0m200), the length of the parentheses string. The second line is a potentially unbalanced parentheses string. Output The minimum number of additional parentheses ("(" or ")") needed to make the corresponding parentheses string balanced, by adding them to any position of the string. You can use the format "printf(" %d ", result);" to output the result of each test case. Sample Input 4 3 (() 3 ((( 2 () 6 ))((0) Sample Output 1304 Explanation There are 4 test cases. For ((), adding one ) to the end to make it balanced: (()). For (( (, adding three ) to the end to make it balanced: ((())). For (), it is already balanced, no parenthesis is needed. For ))(((), adding two ( and two ) to make it balanced: (())(())()Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started