Algorithm:
1. Remove Item i from elements and put in output array
2. Display Output
3. Generate combinations for rest of the array
public void combine(String str){
int l=str.length();
StringBuilder out=new StringBuilder();
char []in=str.toCharArray();
doCombine(in,out,l,0);
}
private void doCombine(char[] in, StringBuilder out, int n, int start){
for(int i=start;i<n;i++){
out.append(in[i]);
System.out.println(out);
if(i<n)
doCombine(in, out, n, i+1);
out.setLength(out.length()-1);
}
}
No comments:
Post a Comment