Search code examples
javastringpermutationpermute

Permutation of more than one string of different lengths in java?


string1 = "ABC" 
string2 = "DEF" 
string3 = "GHIJ"

Output : "ADG", "ADH", "ADI", "ADJ", "AEG", "AEH", "AEI", "AEJ", "AFG", "AFH", "AFI", "AFJ" same way for b and c Using recursive approach would be a great help..


Solution

  • This method doesn't use recursion but is, in my opinion, the easiest way to generate the required permutations.

    static void permute(String... str)
    {    
      char[][] chars = new char[str.length][];
      for(int i=0; i<str.length; i++)
        chars[i] = str[i].toCharArray();
    
      int[] idx = new int[str.length];
    
      char[] perm = new char[str.length];    
      for(int i=0; i<str.length; i++)
        perm[i] = chars[i][0];
    
      while(true)
      {      
        System.out.println(new String(perm));
    
        int k=str.length-1;
        for(; k>=0; k--)
        {
          idx[k] += 1;
          if(idx[k] < chars[k].length) 
          {
            perm[k] = chars[k][idx[k]];
            break;
          }
          idx[k] = 0;
          perm[k] = chars[k][idx[k]];
        }
        if(k < 0) break;
      }
    }
    

    Test:

    public static void main(String[] args)
    {
      permute("ABC", "DEF", "GHIJ");
    }
    

    Output:

    ADG
    ADH
    ADI
    ADJ
    AEG
    AEH
    AEI
    <snip>
    CFG
    CFH
    CFI
    CFJ