-2

Similar to this problem but not sure how to create the algorithm in java.

I have an ArrayList<char[]> preCombination= [['a'],['b','B'],['c','C']] How do I get an output ArrayList with =[['a','b','c'],['a','b','C'],['a','B','c'],['a','B','C']

I have tried to modify this permutation code into a combination one, however, I am unable to get into the inner loop. Appreciate if explanations is simple as I am new to coding, thanks in advance!

public static void permute(ArrayList <char[]> array,int index) {
        if (index==array.size()-1) {
            //System.out.println(array);
        }
        for (int i=index;i<array.size();i++) {
            swap(array,i,index);
            permute(array,index+1);
            swap(array,i,index);
        }
    }
    private static void swap(ArrayList <char[]> input,int a,int b) {
        char[] temp=input.get(a);
        input.set(a, input.get(b));
        input.set(b, temp);
    }
Eugene
  • 1
  • 1
    You don't need permutations or combinations (in combinatorial sense). Just find `Cartesian product` implementation – MBo Sep 09 '21 at 04:47
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 14 '21 at 18:23

1 Answers1

-1

Well, Given that the array that you provided has relatively low number of elements a three way nested loop would do the job, First loop being for row 1 (Mind that row one has just one element so loop really not needed) Second loop for row 2 and consequently Third Loop for row 3. This would not at all be efficient for higher number of elements, but for examples like these, the most crude and easiest format available