Question

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

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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());
     
  
   }
   }
   }

Add a comment
Know the answer?
Add Answer to:
Given a string that consists of only two types of characters “(“ and “)”. Balance the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a client function parenthesesMatch that given a string containing only the characters for parentheses, braces...

    Write a client function parenthesesMatch that given a string containing only the characters for parentheses, braces or curly braces, i.e., the characters in ’([{}])’, returns True if the parentheses, brackets and braces match and False otherwise. Your solution must use a Stack. For, example: >>> parenthesesMatch('(){}[]') True >>> parenthesesMatch('{[()]}') True >>> parenthesesMatch('((())){[()]}') True >>> parenthesesMatch('(}') False >>> parenthesesMatch('({])') False >>> parenthesesMatch('((())') False >>> parenthesesMatch('(()))') False >>> Hint: It is not sufficient to just count the number of opening and closing...

  • Define a word as a string that can only contain alphabetical characters. Given a string of...

    Define a word as a string that can only contain alphabetical characters. Given a string of multiple words, each word is separated by one or multiple spaces (‘ ‘) or tabs (‘\t’). You are asked to count the number of words and find the longest word. In C

  • Anagram Difference We define an anagram to be a word whose characters can be rearranged to...

    Anagram Difference We define an anagram to be a word whose characters can be rearranged to create another word. Given two strings, we want to know the minimum number of characters in either string that we must modify to make the two strings anagrams. If it's not possible to make the two strings anagrams, we consider this number to be -t For example: tea and ate are anagrams, so we would need to modity 0 characters tea and toe are...

  • 2) Hashing functions can be applied to string of characters. For example "amigo" and "annex" can...

    2) Hashing functions can be applied to string of characters. For example "amigo" and "annex" can be assigned hashing value 0, "bike" and "bureaucrat" can be assigned 1, "zebra" can be assigned value 25. So in order to find the hashing value of a word, we find the hashing value of the first letter (ht(a)=0, ht(b)=1, ht(c)=2,….,ht(z)=25). So in this case it is necessary 26 buckets. a) Give an example of another hashing function on string of characters, and determine...

  • recursively take two strings that are NOT THE SAME and return the string that consists of...

    recursively take two strings that are NOT THE SAME and return the string that consists of all the letters that appear in both string1 and string2 using only string1 and string2 as inputs with NO LOOPS. This must be done recursively. for example, if string1 is "fourth" and string2 is "faster" the string returned is "frt"

  • recursively take two strings that are NOT THE SAME and return the string that consists of...

    recursively take two strings that are NOT THE SAME and return the string that consists of all the letters that appear in both string1 and string2 using only string1 and string2 as inputs with NO LOOPS. This must be done recursively. for example, if string1 is "fourth" and string2 is "faster" the string returned is "frt" LANGUAGE IS JAVA

  • Define a function called repeat_middle which receives as parameter one string (with at least one character),...

    Define a function called repeat_middle which receives as parameter one string (with at least one character), and it should return a new string which will have the middle character/s in the string repeated as many times as the length of the input (original) string. Notice that if the original string has an odd number of characters there is only one middle character. If, on the other hand, if the original string has an even number of characters then there will...

  • Python Programming: An individual playing card is represented as a string of two characters: • the...

    Python Programming: An individual playing card is represented as a string of two characters: • the first character is from "23456789TJQKA" and represents the rank, i.e., the number or value of the card. (Note that 10 is encoded as letter T to make all card ranks to be single letters) • the second character is from "cdhs" and represents the suit (clubs, diamonds, hearts and spades respectively). For example, "Jd" would be the jack of diamonds, and "4s" would be...

  • String data and string manipulations are a major capability in any type of data processing. Character...

    String data and string manipulations are a major capability in any type of data processing. Character arrays are rather limited compared to String data types. This application will: 1. Allow the user to enter a string (multiple words including blanks) 2. Determine the number of vowels (a, e, i, o, u) and non vowels.a. Total number of Individual a’s, e’s, i’s, o’s, u’sb. Uppercase vowels count the same as lowercase vowels (i.e. “A” is the same as “a”)c. Any other...

  • You are given a list of strings L = {s1,s2 ...sn}. Each si is a string...

    You are given a list of strings L = {s1,s2 ...sn}. Each si is a string of up to 26 English characters. For example, L consists of the following strings: {s1, 82... Sn}. Each s; is a string of up to 26 English 13. You are given a list of of strings L = characters. For example L consists of the following strings: S1 =hello', S2 = 'world, s3 = 'what', s4 = 'a', 85 = 'nice', s6 = 'day',...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT