This is an interview question: Find all (english word) substrings of a given string. (every = every, ever, very).
Obviously, we can loop over all substrings and check each one against an English dictionary, organized as a set. I believe the dictionary is small enough to fit the RAM. How to organize the dictionary ? As for as I remember, the original spell
command loaded the words
file in a bitmap
, represented a set of words hash values. I would start from that.
Another solution is a trie
built from the dictionary. Using the trie we can loop over all string characters and check the trie
for each character. I guess the complexity of this solution would be the same in the worst case (O(n^2)
)
Does it make sense? Would you suggest other solutions?