import java.util.*;
public class PermStr {
public static void main(String[] args) {
String inp = "0ab";
if (args.length>0) inp = args[0];
for (String str : permStr(inp)){
System.out.println(str);
}
}
static Vector<String> permStr(String str){
if (str.length() == 1){
Vector<String> ret = new Vector<String>();
ret.add(str);
System.out.println(" >ret:"+ret);
return ret;
}
char start = str.charAt(0);
Vector<String> endStrs = permStr(str.substring(1));
Vector<String> newEndStrs = new Vector<String>();
for (String endStr : endStrs){
System.out.println(" >next:"+endStr);
for (int j = 0; j <= endStr.length(); j++){
System.out.println(" >j:"+j);
if (endStr.substring(0, j).endsWith(String.valueOf(start)))
break;
System.out.println(" >endStr.substring(0, j)"+endStr.substring(0, j));
System.out.println(" >String.valueOf(start)"+String.valueOf(start));
System.out.println(" >endStr.substring(j);:"+endStr.substring(j));
newEndStrs.add(endStr.substring(0, j) + String.valueOf(start) + endStr.substring(j));
}
}
System.out.println(" >>return newEndStrs: "+newEndStrs);
return newEndStrs;
}
}
0ab
a0b
ab0
0ba
b0a
ba0
Sunday, March 13, 2011
Permutations
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment