Here you can find the source of permute(String str, int startIndex, int endIndex)
public static void permute(String str, int startIndex, int endIndex)
//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); } }