Java Array Permute permute(String str, int startIndex, int endIndex)

Here you can find the source of permute(String str, int startIndex, int endIndex)

Description

Algo {A,B,C} first take index 0 and swap with all A swap with A {A,B,C} ---> now take second char and make first char fixed {A,B,C} and {A,C,B} A swap with B {B,A,C} ---> now take second char and make first char fixed {B,A,C} and {B,C,A} A swap with C {C,B,A} ---> now take second char and make first char fixed {C,B,A} and {C,A,B} now take second char on each

License

Apache License

Declaration

public static void permute(String str, int startIndex, int endIndex) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from   w ww.  j  a  v a2  s.c om
     * Algo {A,B,C}
     * first take index 0 and swap with all
     * A swap with A {A,B,C} ---> now take second char and make first char fixed {A,B,C} and {A,C,B}
     * A swap with B {B,A,C} ---> now take second char and make first char fixed {B,A,C} and {B,C,A}
     * A swap with C {C,B,A} ---> now take second char and make first char fixed {C,B,A} and {C,A,B}
     * now take second char on each
     * 
     */
    public static void permute(String str, int startIndex, int endIndex) {
        if (startIndex == endIndex) {
            System.out.println(str);
        } else {
            for (int i = startIndex; i <= endIndex; i++) {
                str = swap(str, startIndex, i);
                permute(str, startIndex + 1, endIndex);
                str = swap(str, startIndex, i);
            }
        }
    }

    public static String swap(String str, int i, int j) {
        char[] chars = str.toCharArray();
        char temp = chars[i];
        chars[i] = chars[j];
        chars[j] = temp;
        return String.valueOf(chars);
    }
}

Related

  1. permutation(int n)
  2. permute(int k, Object[] os)
  3. permute(int[] elements, int i, int j)
  4. permute(int[] in, int[] idx)
  5. permute(int[] p, Object[] data, boolean clone)
  6. permute(T[] array)
  7. permute(T[] values)
  8. permuteArray(int[] array, Integer[] permutation)
  9. permuteCols(double[][] ary, int[] idx)