Possible Duplicate:
Generating all permutations of a given string
Given a length n=4,
and a set of characters -> {'a', 'b'}
,
how to write some java codes to produce all the possible string with that length n containing the characters in the set?
For the example above, the result should have 2^4=16 strings, which is:
aaaa
aaab
aabb
abbb
baaa
baab
babb
bbbb
bbaa
bbab
bbba
abaa
abab
abba
baba
aaba
here is my code snippet:
public void process(String result, String string)
{
if(string.length() == 0)
{
System.out.println(result);
}else{
for(int i = 0; i < string.length(); i++)
{
String newResult = new String(result+string.charAt(i));
String newString = new String(string.substring(0,i) + string.substring(i+1, string.length()));
process(newResult, newString);
}
}
}
That seems like only doing permutation, instead of what I want....... Thank you in advance :)