The way I am thinking of this is, say you have the word "palindrome". From that word, and given the dictionary of possible words which I have learned ("my dictionary"), you can generate:
do
in
drone
mind
plan
line
role
lad
pad
drape
...
Now imagine we do this programmatically, given a dictionary in computer form (a list of words in a text file). The question is how to somewhat efficiently find the words that can be generated from a word, for each word. So for every word in the dictionary, we find every word that can be generated from it.
The rules for generating the words from a given word are that you can only use each letter once, but you can pick which letter you want in any order.
The naive way I am starting to try to solve this problem is by first loading all words into a hashtable, an object in JavaScript. Seems like maybe putting them into a trie would be better, but not sure. Then iterate through each word in the hashtable. Try generating all possible combinations of letters of all possible lengths for the given word. I have no idea how to mathematically calculate how many combinations this is, but it seems like a lot. So for "palindrome" that is 10 letters, but seems like a ton of combinations just for 10 letter combinations, not to mention 9, 8, 7, 6....
So wondering how you might go about this more efficiently. I'm sure someone has already found the trick to solve this nicely.
Note, this is not a homework or interview question, I am just curious how to do this efficiently.
10!
, or3,628,800
. Of course, for 11 letters it would be close to 40 million. You definitely don't want to do an exhaustive search . . . – Jim Mischel