You could use recursion to do this.
-
Try each of the letters in turn as the first letter and then find all the permutations of the remaining letters using a recursive call.
-
The base case is when the input is an empty string the only permutation is the empty string
Here's how you could probably implement it:
public class PermutationsTest {
public static void main(String[] args) throws Exception {
String str = "sample";
StringBuffer strBuf = new StringBuffer(str);
doPerm(strBuf,0);
}
private static void doPerm(StringBuffer str, int index){
if(index == str.length())
System.out.println(str);
else { //recursively solve this by placing all other chars at current first pos
doPerm(str, index+1);
for (int i = index+1; i < str.length(); i++) {//start swapping all other chars with current first char
swap(str,index, i);
doPerm(str, index+1);
swap(str,i, index);//restore back my string buffer
}
}
}
private static void swap(StringBuffer str, int pos1, int pos2){
char c1 = str.charAt(pos1);
str.setCharAt(pos1, str.charAt(pos2));
str.setCharAt(pos2, c1);
}}