Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smart way to generate permutation and combination of String

String database[] = {'a', 'b', 'c'};

I would like to generate the following strings sequence, based on given database.

a
b
c
aa
ab
ac
ba
bb
bc
ca
cb
cc
aaa
...

I can only think of a pretty "dummy" solution.

public class JavaApplication21 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        char[] database = {'a', 'b', 'c'};

        String query = "a";
        StringBuilder query_sb = new StringBuilder(query);
        for (int a = 0; a < database.length; a++) {
            query_sb.setCharAt(0, database[a]);
            query = query_sb.toString();                    
            System.out.println(query);            
        }

        query = "aa";
        query_sb = new StringBuilder(query);
        for (int a = 0; a < database.length; a++) {
            query_sb.setCharAt(0, database[a]);    
            for (int b = 0; b < database.length; b++) {    
                query_sb.setCharAt(1, database[b]);    
                query = query_sb.toString();                    
                System.out.println(query);
            }
        }

        query = "aaa";
        query_sb = new StringBuilder(query);
        for (int a = 0; a < database.length; a++) {
            query_sb.setCharAt(0, database[a]);    
            for (int b = 0; b < database.length; b++) {    
                query_sb.setCharAt(1, database[b]);    
                for (int c = 0; c < database.length; c++) {                    
                    query_sb.setCharAt(2, database[c]);                        
                    query = query_sb.toString();                    
                    System.out.println(query);
                }
            }
        }
    }
}

The solution is pretty dumb. It is not scale-able in the sense that

  1. What if I increase the size of database?
  2. What if my final targeted print String length need to be N?

Is there any smart code, which can generate scale-able permutation and combination string in a really smart way?

like image 584
Cheok Yan Cheng Avatar asked Nov 15 '13 03:11

Cheok Yan Cheng


People also ask

How do you calculate permutations of a string?

The number of permutations of n objects taken r at a time is determined by the following formula: P(n,r)=n! (n−r)!


Video Answer


3 Answers

You should check this answer: Getting every possible permutation of a string or combination including repeated characters in Java

To get this code:

public static String[] getAllLists(String[] elements, int lengthOfList)
{

    //lists of length 1 are just the original elements
    if(lengthOfList == 1) return elements; 
    else {
        //initialize our returned list with the number of elements calculated above
        String[] allLists = new String[(int)Math.pow(elements.length, lengthOfList)];

        //the recursion--get all lists of length 3, length 2, all the way up to 1
        String[] allSublists = getAllLists(elements, lengthOfList - 1);

        //append the sublists to each element
        int arrayIndex = 0;

        for(int i = 0; i < elements.length; i++){
            for(int j = 0; j < allSublists.length; j++){
                //add the newly appended combination to the list
                allLists[arrayIndex] = elements[i] + allSublists[j];
                arrayIndex++;
            }
        }
        return allLists;
    }
}

public static void main(String[] args){
    String[] database = {"a","b","c"};
    for(int i=1; i<=database.length; i++){
        String[] result = getAllLists(database, i);
        for(int j=0; j<result.length; j++){
            System.out.println(result[j]);
        }
    }
}

Although further improvement in memory could be made, since this solution generates all solution to memory first (the array), before we can print it. But the idea is the same, which is to use recursive algorithm.

like image 143
justhalf Avatar answered Oct 04 '22 12:10

justhalf


This smells like counting in binary:

  • 001
  • 010
  • 011
  • 100
  • 101
  • ...

My first instinct would be to use a binary counter as a "bitmap" of characters to generate those the possible values. However, there are several wonderful answer to related questions here that suggest using recursion. See

  • How do I make this combinations/permutations method recursive?
  • Find out all combinations and permutations - Java
  • java string permutations and combinations lookup
  • http://www.programmerinterview.com/index.php/recursion/permutations-of-a-string/
like image 35
dj_segfault Avatar answered Oct 04 '22 11:10

dj_segfault


Java implementation of your permutation generator:-

public class Permutations {


    public static void permGen(char[] s,int i,int k,char[] buff) {
        if(i<k) {
            for(int j=0;j<s.length;j++) {

                buff[i] = s[j];
                permGen(s,i+1,k,buff);
            }
        }       
        else {

         System.out.println(String.valueOf(buff)); 

        }

    }

    public static void main(String[] args) {
        char[] database = {'a', 'b', 'c'};
        char[] buff = new char[database.length];
        int k = database.length;
        for(int i=1;i<=k;i++) {
            permGen(database,0,i,buff);
        }

}

}
like image 38
Vikram Bhat Avatar answered Oct 04 '22 13:10

Vikram Bhat