Starting with alphabetized key, 'aehrt', you can find all possible combinations of letters using the following method:
- start with:
S0 = {}
- next, take
a
:S1 = a⋅S0 ∪ S0 ∪ {a} = {a}
- next, take
e
:S2 = e⋅S1 ∪ S1 ∪ {e} = {ae, a, e}
- next, take
h
:S3 = h⋅S2 ∪ S2 ∪ {h} = {aeh, ah, eh, ae, a, e, h}
- etc...
once you have S5
(the entire set of combinations) check them all against your map.
public static void main(String... args){
Set<String> set = new TreeSet<String>();
String key = "aehrt";
//S1 = c*S0 ∪ {c} ∪ S0
for(int z = 0; z < key.length();z++) {
Set<String> temp = new HashSet<String>();
char c = key.charAt(z);
for(String str: set)
temp.add(str + c); // ∪ c*S0
set.add(c+""); // ∪ {c}
set.addAll(temp); // ∪ S0
}
System.out.println(set);
}
output: [a, ae, aeh, aehr, aehrt, aeht, aer, aert, aet, ah, ahr, ahrt, aht, ar, art,
at, e, eh, ehr, ehrt, eht, er, ert, et, h, hr, hrt, ht, r, rt, t]