Good algorithm and data structure for looking up words with missing letters?

前端 未结 20 1942
不思量自难忘°
不思量自难忘° 2020-12-07 07:12

so I need to write an efficient algorithm for looking up words with missing letters in a dictionary and I want the set of possible words.

For example, if I have th??

20条回答
  •  误落风尘
    2020-12-07 07:31

    If you generate all the possible words that match the pattern (arate, arbte, arcte ... zryte, zrzte) and then look them up in a binary tree representation of the dictionary, that will have the average performance characteristics of O(e^N1 * log(N2)) where N1 is the number of question marks and N2 is the size of the dictionary. Seems good enough for me but I'm sure it's possible to figure out a better algorithm.

    EDIT: If you will have more than say, three question marks, have a look at Phil H's answer and his letter indexing approach.

提交回复
热议问题