How to implement recursive all possible combination of any set (JAVA)
Question: Can someone give me a few clues or help with writing a combination function
where it would output all possible combination of a set. I have an idea. But I
find it hard.

Something like this in Java.

    
    
    String set[] = {"Java","C++","Python"};
    
    ArrayList<String> list = new ArrayList<String>();
    
    public void combination(String[] set) {
    
        if (set.length == 0) {
    
           do nothing
    
        } else if (this a combination from the set) {
           add to the list
        }
    }
    


Answer: Here's a simple example of combinations of all letters in a String, just to
give you an algorithm. See if you can transfer the algorithm to your example.

    
    
        public class StringLib {
    
        public static void combine(String str){
            int length=str.length();
    
            StringBuffer output=new StringBuffer();
    
            combination(str,length,output,0);
        }
    
        static void combination(String str, int length, StringBuffer output, int level){
            /* show arms-length recursion style with better peformance */
            if (level==length)
                return;
            else{
                for(int i=level;i<length;i++){
                    output.append(str.charAt(i));
                    System.out.println(output.toString());
                    combination(str,length,output,i+1);
                    output.deleteCharAt(output.length()-1);
                }
            }
        }
    
    
        public static void main(String[] args){
            combine("abc");
        }
    }
    
    /*Output:
    
    a
    ab
    abc
    ac
    b
    bc
    c
    */
    

Just in case you actually need permutations:

    
    
    import java.util.*;
    
    public class permutations
    {
      public static void main(String[] args)
      {
        String str = new String("abc");
        StringBuffer output = new StringBuffer();
    
        boolean used[] = {false, false, false};
    
        permute.printPermutations(0, output, used, str);
      }
    }
    
    
    public class permute{
    
      static void printPermutations(int level, StringBuffer output, boolean used[], String orig){
          int len = orig.length();
          if (level == len){
              System.out.println(output);
          }
          else{
              for (int i = 0; i < len; i++){    
                  //recurse if not used already
                  if (!used[i]){
                      output.append(orig.charAt(i));
                      used[i] = true;
                      printPermutations(level+1, output, used, orig);
                      output.deleteCharAt(output.length() - 1);
                      used[i] = false;
                  }
              }
          }
      }
    }
    
    
    
    
    /*Output:
    
    abc
    acb
    bac
    bca
    cab
    cba
    
    */
    

