Hi I'm trying to figure out an efficient way to get all permutations of a string but also allowing repetition of characters. (for example for string "ab" the output would be "aa, bb, ba, ab")
I have my code working correctly without repetition but I'm not sure how to go about modifying it to allow repetition of characters and only seem to find ways of doing this without repetition of characters.
Here is the code that I have:
public static ArrayList<String> permutationsUnique(String word) {
ArrayList<String> result = new ArrayList<String>();
if (word.length() == 0) {
result.add(word);
return result;
} else {
for (int i = 0; i < word.length(); i++) {
String shorter = word.substring(0, i) + word.substring(i + 1);
ArrayList<String> shorterPermutations = permutationsUnique(shorter);
for (String s : shorterPermutations) {
result.add(word.charAt(i) + s);
}
}
return result;
}
}
I would really appreciate if some one can guide me in the right direction.
Thank you
"bba","aba", and"abb"are also permutations created with just the letters'a'and'b'.