I am trying to find a quick way to get all possible permutations of a word. Right now I am Using Guava, but it kills my RAM for words with >10 characters. My current approach is:
List<String> list = new ArrayList();
for(char c : word.toCharArray()){
list.add(String.valueOf(c));
}
Collection<List<String>> substrings = Collections2.orderedPermutations(list);
List<String> cleanedList = new ArrayList<>();
substrings.stream()
.forEach(f-> {
String concat = "";
for(int i = 0; i < f.size(); i++){
concat += f.get(i);
}
cleanedList.add(concat);
});
The result is fine, e.g. for "Wei":
Thats exactly what I want but I need it more efficiently. Is there a way? I mean I kind of get the problem as 4 characters gets 24 options and 7 characters already produces 840.. It probably be a good start to have a function that realizes if characters are the same and won't set a character in a position another character of the same type was already in.