I am writing a game which when given a partially filled word, searches a dictionary and returns all the matching words. To that effect, I am trying to find an algorithm that can be used for the said purpose. For example, given – – a -, the algorithm will search a dictionary for all the words which have length 4 and have ‘a’ as the third letter.
Is there such an algorithm already? If not, can somebody given a rough idea of how to design such an algorithm?
Thanks in Advance.
Well, it does not already exists, but it’s been researched on SO already, for crosswords problems.
The gist of the solution I proposed was to index by letter and indexes, which is Python gives:
The idea is simple: you have a large index which has a set of words for each couple
(position,letter). It could be extended, in your case, to have one index per word length, which would dimish the size of the sets of words and thus be faster.For retrieval, you simply intersect all the sets to have the common set of word that matches all the known letters.