string - JAVA for creating all possible combination of words -


i trying possible combinations of words input user. such words input aa, bb, cc should give

aa bb cc aabb aacc bbaa bbcc ccaa ccbb aabbcc aaccbb bbaacc bbccaa ccbbaa ccaabb 

they can in order preferably in sorted order.

i have been trying accomplish past hour , think confused can't figure out , keep going in circles. can please point me in right direction.

the code far

import java.util.arrays; import java.util.scanner;  public class wordcombination { private static string[] wordlist; private static string[] wordcomb;  public static void main(string[] argv){     scanner = new scanner(system.in);      system.out.print("enter words:");     setwords(a.nextline());      creatcombinations();  }  private static void setwords(string words){     system.out.println("entered words: " + words);     wordlist = words.split("\\s+");     arrays.sort(wordlist);     system.out.println(arrays.tostring(wordlist)); }  private static void creatcombinations(){     int size = wordlist.length;     int perm = (int) math.pow(size, size);     int c = 1;      wordcomb = new string[perm];      string word = "";     /*     for(int = 0; < size; i++){         word = wordlist[i];                  for(int j = 0; j < size; j++){             word += wordlist[j];                         system.out.println(c + ": " + word + ".com");             c++;         }         word = "";     }*/     int l = 0;     for(int = 0; < size; i++){         int permj = (i+1) * size;         system.out.println("------------------> " + permj + " permutations " + (i + 1) + " words combination");         int iterations = permj * (i+1);         system.out.println(" iterations: " + iterations);         word = wordlist[i];         for(int j = 0; j < permj; j++){             for(int k = 0; k < i+1; k++){                 int permi = * size;                 int index = permi + (k%permj);                 wordcomb[c-1] += "" + wordlist[l%size];                 out(l%size + "");                 l++;             }             system.out.println(c + ": " + wordcomb[c-1]);             c++;         }         word = "";     }  }  private static void out(string s){     system.out.println(s); } 

}

*edit: output getting *

    enter words:1 2 3 entered words: 1 2 3 [1, 2, 3] ------------------> 3 permutations 1 words combination  iterations: 3 0 1: 1 1 2: 2 2 3: 3 ------------------> 6 permutations 2 words combination  iterations: 12  enter words:aa bb cc entered words: aa bb cc [aa, bb, cc] ------------------> 3 permutations 1 words combination  iterations: 3 0 1: aa 1 2: bb 2 3: cc ------------------> 6 permutations 2 words combination  iterations: 12 0 1 4: aabb 2 0 5: ccaa 1 2 6: bbcc 0 1 7: aabb 2 0 8: ccaa 1 2 9: bbcc ------------------> 9 permutations 3 words combination  iterations: 27 0 1 2 10: aabbcc 0 1 2 11: aabbcc 0 1 2 12: aabbcc 0 1 2 13: aabbcc 0 1 2 14: aabbcc 0 1 2 15: aabbcc 0 1 2 16: aabbcc 0 1 2 17: aabbcc 0 1 2 18: aabbcc 

i think approach fundamentally flawed. in output, first character of permutation = last character of previous 1 + 1, , each character of permutation = previous character + 1, not how permutation work. don't think can fixed.

below working way it, using recursion. tries every character in position, recurses next position.

import java.util.arrays;  public class newmain {    public static void main(string[] args)    {       c = arrays.aslist("aa","bb","cc").toarray(new string[0]);       permutation(0);       system.out.println("number of permutations = " + count);    }     static string[] c;    static int count = 0;     static void swap(int pos1, int pos2)    {       string temp = c[pos1];       c[pos1] = c[pos2];       c[pos2] = temp;    }     public static void permutation(int start)    {       if (start != 0)       {          (int = 0; < start; i++)             system.out.print(c[i]);          system.out.println();          count++;       }        (int = start; < c.length; i++)       {          swap(start, i);          permutation(start + 1);          swap(start, i);       }    } } 

prints:

aa aabb aabbcc aacc aaccbb bb bbaa bbaacc bbcc bbccaa cc ccbb ccbbaa ccaa ccaabb number of permutations = 15 

Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -