I am trying to generate all the unique strings, that can be created by removing one or more characters from the original string.
For example, for the original string "anna", the resulting strings would be nna|ana|ann|na|aa|nn|an|a|n. I am not simply looking for unique substrings, as for example "aa" is not a substring of "anna", yet it is included in the set of the strings I am looking for.
The code below generates the correct result, but is too slow for when the original string is extraordinarily long, for example 50 characters or so. I am looking for suggestions on how to make it more efficient.
public class Permutation3 {
private static Set<String> hs = new HashSet<>();
public static void main(String[] args) {
perm("ANNA", 0);
System.out.println(hs.size() - 2);
}
private static void perm(String string, int startIndex) {
hs.add(string);
for (int i = startIndex; i <= string.length() - 1; i++) {
StringBuilder sb = new StringBuilder(string);
sb.deleteCharAt(i);
perm(sb.toString(), i);
}
}