In: Computer Science
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, generateSubstrings("ABC", result); adds to result the strings: "", "A", "B", "C", "AB", "AC", "BC", "ABC" The order of the strings does not matter. You may use iteration for this problem Use Java
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
import java.util.ArrayList;
public class ArrayListSubStrings
{
    private static void generateSubstrings(String str, ArrayList<String> result)
    {
        // take length of string
        int n=str.length();
        // repeat till 2^n
        for(long i=0;i<(1<<n);i++)
        {
            // take temporary string
            String temp="";
            for(int j=0;j<n;j++)
            {
                // generrate the sequance
                if( (i & (1<<j))>0)
                    temp+=str.charAt(j);
            }
            // add that temporary string to the array list
            result.add(temp);
        }
    }
    // test your method
    public static void main(String[] args) {
        ArrayList<String> result=new ArrayList<String>();
        // call the method
        generateSubstrings("ABC",result);
        // print the output
        System.out.println("The substrings are: "+result);
    }
}
=======

