The obvious algorithm (just replacing letters) is a bit naive and does not consider the same letters as instances of the same letter. For example, if you have the word "eve", the two "e" are different from each other; if we single out the first E for illustration, you get combinations of " e ve" and "ev e " at different points in the process.
You need to somehow eliminate duplicates. The easiest way to do this is to assemble combos into a set of some types (for example, HashSet). It can contain only one element, so duplicates will be deleted.
Oh, and use String s, not StringBuilder s. I just noticed that you are doing this. StringBuilder does not override equals , so you are left with a version inherited from Object . Final result: for two StringBuilders a and b , a.equals(b) only if a == b .
cHao source share