Code For NonGeek
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Permutations of a String

Go down

Permutations of a String Empty Permutations of a String

Post  skyboard Sat Oct 01, 2011 6:51 pm


public class Permutation {
public void permute(String str){
int length=str.length();
boolean[] used=new boolean[length];
StringBuilder out=new StringBuilder();
char[] in =str.toCharArray();
doPermute(in, out,used, length,0);
}

public void doPermute(char[] in, StringBuilder out, boolean[] used, int length, int level){
if(level==length){
System.out.println(out.toString());
return;
}
for(int i=0;i<length;i++){
if(used[i])
continue;
out.append(in[i]);
used[i]=true;
doPermute(in,out,used,length,level+1);
used[i]=false;
out.setLength(out.length()-1);
}
}

public static void main(String[] args){
Permutation p=new Permutation();
p.permute("abcd");
}



=======================================
What is the use of

out.setLength(out.length()-1);

skyboard

Posts : 31
Join date : 2011-09-03

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum