Permutation Of Given Word

public class Permutation {

    public static void main(String[] args) {
        String str = "ABCD";        int len = str.length();        Permutation permutation = new Permutation();        permutation.permute(str, 0, len-1);    }

    private void permute(String str, int left, int right) {
        if(left == right){
            System.out.println(str);        }else{
            for(int i=left; i<=right; i++){
                str = swap(str,left,i);                permute(str, left+1, right);            }
        }
    }

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

Comments