1 answer

Given a string that consists of only two types of characters “(“ and “)”. Balance the...

Question:

Given a string that consists of only two types of characters “(“ and “)”. Balance the parentheses by inserting either a “(“ or a “)” as many times as necessary. Determine the minimum number of characters that must be inserted.


Answers

public class BalancetheParantheses
{
  
   public static void main(String args[])
   {
   String str = " ";
   Stack<Character> s= new Stack(); // created stack s for --> ( <-- character
   Stack<Character> s1=new Stack(); // created stack s1 for --> ) <-- character
   // Creating array of string length
   char[] ch = new char[str.length()];
   char[] ch1=new char[str.length()];
   int count=0;
   int count1=0;
   int m=0;
   int m1=0;
   if(str==null|str.isEmpty())
   {System.out.println("Empty please fill the value "); }
   else {
   for (int i = 0; i < str.length(); i++)
   {
   if(str.charAt(i)=='(')
   {
       s.push(str.charAt(i));
       count++;
         
   }
   else if(str.charAt(i)==')')
   {
       s1.push(str.charAt(i));
       count1++;
   }
  
     
   }
  
  
   if(count==count1)
   {
       System.out.print("Your String is Balanced \n");
   }
  
   else {
      
  
   if(count>count1)
   {
       while(count>count1)
       {
           s1.push(')');
           count1++;
           m++;
       }
   }
   else if (count1>count)
   {
       s.push('(');
       count++;
       m++;
   }
  
   }
     
   System.out.println("Minimum Number of Characters must Inserted :" +m);
   System.out.println("The Resultant String : \n"+ s.toString()+s1.toString());
     
  
   }
   }
   }

.

Similar Solved Questions

1 answer
2. (2 marks) Consider a pre-loaded threaded bolted joint subjected to static load P as shown...
2. (2 marks) Consider a pre-loaded threaded bolted joint subjected to static load P as shown in the figure below. This figure shows one of the bolts of a threaded joint that is part of a group of uniformly loaded bolts. The bolt is a SA Grade 4 bolt having a major diameter d0.375 in and the pitch p ...
1 answer
Coolplay Corp is thinking about opening a soccer camp in southern California. To start the camp,...
Coolplay Corp is thinking about opening a soccer camp in southern California. To start the camp, Coolplay would need to purchase land and build four soccer fields and a sleeping and dining facility to house 150 soccer players. Each year, the camp would be run for 8 sessions of 1 week each. The compa...
1 answer
Gazin Inc. was in business for four years. The following table shows its purchases and sales...
Gazin Inc. was in business for four years. The following table shows its purchases and sales during that four-year period. Purchases Units Purchases Unit Costs Sales Units Sales Unit Costs Year 1 12,000 $10 9,000 $15 Year 2 11,000 $11 10,000 $17 Year 3 10,000 $12 11,000 $19 Year 4 9,000 ...
1 answer
PROBLEM i Create a column of monthly returns for your 2 stocks and the following 3...
PROBLEM i Create a column of monthly returns for your 2 stocks and the following 3 portfolios. Organize your spreadsheet as follows: a. Date VRSN (#1) Portfolio 1 80% in A 20% in B Portfolio 2 50% in A 50% in B Portfolio 3 20% in A 80% in B MNST (#2) S&P 500 X.x X.x Xx Xx Xx b. Calculate the his...
1 answer
12. At high temperatures, limestoned CaCO3 (s) e CaO (s) + CO2 (g,) If this reaction...
12. At high temperatures, limestoned CaCO3 (s) e CaO (s) + CO2 (g,) If this reaction is run at 900°C and reaches equilibrium when [CO21-0.76 M, what is the value of Kp at this temperature? A. 0.76 B. 73 C. 56 D. 0.008 E. 96...
1 answer
2. A deck of 52 cards is shuffled. The first five cards of the deck are...
2. A deck of 52 cards is shuffled. The first five cards of the deck are red. What is the probability that the last card in the deck is the ace of spades? 3. Let A, B and C be events in a sample space, with P(C) + 0. Prove or disprove the following statements. (a) If A and B are independent, then A a...
1 answer
Plz ASAP! written clearly! thank you so much ! 2 Forthe system shown K=576lbia, M=4 hsin....
plz ASAP! written clearly! thank you so much ! 2 Forthe system shown K=576lbia, M=4 hsin. a. The equation of motion and the response Flt) asfinction oftime when C=192 sin, and is subjected to barmonic force F(t) Cos (10 b. Rescrance freguency. Stow your work...
1 answer
Figure 10-3 Supply Price (dollars per ticket) 10 Demand 200 Quantity (tickets per day) Bettye Babylon...
Figure 10-3 Supply Price (dollars per ticket) 10 Demand 200 Quantity (tickets per day) Bettye Babylon does not charge admission to her arboreal estate, the Hanging Gardens. Due to space limitations, Bettye only allows 200 visitors per day at her estate. The figure represents the supply and demand fo...
1 answer
(a) Elmax Africa is considering the adoption of a new robust Information System, [a computer-based information...
(a) Elmax Africa is considering the adoption of a new robust Information System, [a computer-based information system] for its operations; and ASME IT Solutions is contracted to develop this system. As the Senior System Analyst at ASME IT Solutions you are selected to lead this project. (a). Discuss...
1 answer
Public class Song { private String title; private String artist; private int duration; public Song() {...
public class Song { private String title; private String artist; private int duration; public Song() { this("", "", 0, 0); } public Song(String t, String a, int m, int s) { title = t; artist = a; duration ...
1 answer
How can the railroad be seen as a “technology of settler colonialism” in the United States...
How can the railroad be seen as a “technology of settler colonialism” in the United States and Canada in the late 19th and early 20th centuries? [Answer in a short paragraph - for my course in geography]...
1 answer
How does level of self esteem typically change during middle childhood?
How does level of self esteem typically change during middle childhood?...
1 answer
In the figure, a rectangular loop of wire with length a = 3.8 cm, width b...
In the figure, a rectangular loop of wire with length a = 3.8 cm, width b = 1.3 cm, and resistance R = 0.71 m? is placed near an infinitely long wire carrying current i = 6.7 A. The loop is then moved away from the wire at a constant speed v = 4.8 mm/s. When the center of the loop is at distance r =...