Question

In: Computer Science

Write a static method called generateSubstrings that adds all subsets of the letters in its first...

Write a static method called generateSubstrings that adds all subsets of the letters in its first argument to the ArrayList that is its second argument. For example,

Solutions

Expert Solution

FOLLOW THE COMMENTS IN CODE TO UNDERSTAND THE CODE BEHIND THE FUNCTION.

COMMENT IF YOU GO THROUGH ANY PROBLEM RELATED TO THE QUESTION AND MY SOURCE CODE

private static void generateSubstrings(String text,ArrayList<String> arrlist)
   {
       /* THIS PRIVATE FUNCTION ASSUMES THE FIRST ARHGUMENT TO
       BE A STRING AND THE SECOND AN ARRAYLIST TO WHICH ALL STRINGS
       IN THE FORM OF SUBSETS CAN BE ADDED TO IT*/
       /*
      
       To test the code I have used the extra indentation.
       Our idea is following
      
       We remove duplicates from the String text
      
       We find the total number of possibilities i.e 2 ^ length
       of the string without duplicates
      
       Run through each and every possibility.Our idea is that the
       binary no of the loop variable for each possibilities has one-to-one
       correspondence with the subset to be found. suppose that
       the string without duplicates is "abc" ane the loop variable is 5
       whose binary representation is 101 . In that case we will
       take the first element and last element of the string without duplicates
       i.e the place where '1' matches .we need to add the zeros
       before the binary_representation so as to match the length of the string without
       duplicates so that we can easily find the subset for
       that particular loop variable
       000 in this case is the empty set
       */
      
       //Remove duplicates from the string "text"
       String no_duplicates="";
       for(int i=0;i<text.length();i++)
       {
           boolean flag=true;
           for(int j=i+1;j<text.length();j++)
               if(text.charAt(j)==text.charAt(i))
                   flag=false;
           if(flag)
               no_duplicates=no_duplicates+text.charAt(i);
       }
      
       //Calculate no of possibilities
       int end_no=(int)Math.pow(2,no_duplicates.length());
      
       //The loop that adds the subsets in form of strings
       for(int i=0;i<end_no;i++)
       {
           String bin_no=Integer.toBinaryString(i);
           //Adding extra zeros
           int no_of_extra_zeros=no_duplicates.length()-bin_no.length();
           for(int j=0;j<no_of_extra_zeros;j++)
               bin_no="0"+bin_no;
          
          
           String generated_strings="";
           for(int j=0;j<bin_no.length();j++)
           {
               if (bin_no.charAt(j)=='1')
               generated_strings=generated_strings+no_duplicates.charAt(j);
           }
           arrlist.add(generated_strings);
       }
   }


Related Solutions

Write a static method called "evaluate" that takes a string as a parameter
In Java language  Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
Write a static method called max that creates a Scanner and connects to a file “data.txt”....
Write a static method called max that creates a Scanner and connects to a file “data.txt”. Use an exception handling  mechanism of your choice. The method should read the file until the end of time and consume tokens that could be integer,  double or string type. It should count and return the number of words that are not integer or doubles.
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str). May an array of...
Write a static method startsWith that inputs two Strings and returns a boolean. If the first...
Write a static method startsWith that inputs two Strings and returns a boolean. If the first input starts with the substring that is the second input, then the method returns true; otherwise, it returns false. For example, startsWith( "radar installation", "rad" ) returns true startsWith( "radar installation", "installation" ) returns false startsWith( "radar installation", "" ) returns true startsWith( "", "a" ) returns false startsWith( "", "" ) returns true
Write a static method endsWith that inputs two Strings and returns a boolean. If the first...
Write a static method endsWith that inputs two Strings and returns a boolean. If the first input ends with the substring that is the second input, then the method returns true; otherwise, it returns false. For example, endsWith( "radar installation", "rad" ) returns false endsWith( "radar installation", "installation" ) returns true endsWith( "radar installation", "" ) returns true endsWith( "", "a" ) returns false endsWith( "", "" ) returns true
Java Programming Using the class below, please ), write a static method called parse that parses...
Java Programming Using the class below, please ), write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1 parse(“{ { 4*X}”)...
in Java language, in most simple algorithm Using a stack class, write a static method called...
in Java language, in most simple algorithm Using a stack class, write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1...
How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1,...
How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str)....
(JAVA) ExceptionSum The objective of this exercise is to create a method that adds all the...
(JAVA) ExceptionSum The objective of this exercise is to create a method that adds all the numbers within an array with an exception you cant sum the 6, and the 7, and the numbers between them cannot be added too, taking into account that whenever a 6 appears will be followed by a 7. If we have an array such that [1,2,3,6,10,9,8,7,5] when we pass it through the method it will give us an int such that the sum will...
Write a C++ function that lets the user enter alphabet letters into a static char array...
Write a C++ function that lets the user enter alphabet letters into a static char array until either the user enters a non-alphabet letter or, it has reached the MAXSIZE. You can use the isalpha([Char]) function to check if the input is an alphabet letter or not. void fillArray (char ar[], size_t& size){ // this is the function prototype }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT