Permutations of a String

View previous topic View next topic Go down

Permutations of a String

Post  skyboard on 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

View user profile

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

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