recursion - Generating Power Set of a String Recursively in Java -
i'm trying recursive implementation of power set generator working off of pseudocode given, when given string "abc", rather having sets
{}, {a}, {b}, {c}, {a,b}, {a,c}, {b,c}, , {a,b,c},
i {}, {0}, {1}, {2}, {0,1}, etc.
public static arraylist generatesubsets(string setstring) { arraylist = new arraylist<string>(); arraylist temp = new arraylist<string>(); if(setstring.length() > 0) { temp = generatesubsets(setstring.substring(0,setstring.length() - 1)); for(int = 0; < temp.size(); i++) { system.out.println("temp i: "+temp.get(i)); a.add(temp.get(i)); a.add(temp.get(i) + " " + (setstring.length() - 1)); } return a; } else a.add(""); return a; }
this based directly on pseudocode, why isn't working correctly?
edit: test
public static void main(string[] args) { arraylist 1 = generatesubsets("abcd"); for(int = 0; < one.size(); i++) { system.out.print(one.get(i)+ ", "); if(i%5 == 0) { system.out.println(""); } }
}
and output of (without line breaks)
, 3, 2, 2 3, 1, 1 3, 1 2, 1 2 3, 0, 0 3, 0 2, 0 2 3, 0 1, 0 1 3, 0 1 2, 0 1 2 3,
statement (setstring.length() - 1)
gives index of char. , concatenating receive power set of indexes. need use setstring.charat(setstring.length()-1)
receive char @ given position.