alternative solution
Actually when working on this problem my first solution was different from the author's:
public void combine(int i)
{
if (i == in.length())
System.out.println(out);
else {
combine(i + 1);
out.append(in.charAt(i));
combine(i + 1);
out.setLength(out.length() - 1);
}
}
In this case the function combine(i) should print all combinations in the substring (i, in.length()).
From the example "wxyz" the set of all combinations of "wxyz" is the set of all combinations of "xyz" plus the same set where each combinations is prepended with "w".
The only difference is that the function above will also print empty string as one of the outputs (i was thinking in terms of power sets and power set includes empty set as one of its elements).
Last edited by yevvi; June 22nd, 2014 at 10:59 PM..
|