This is an interview question. Suppose you have a string text and a dictionary (a set of strings). How do you break down text into substri
Approach 1-
Trie looks to be a close fit here. Generate trie of the words in english dictionary. This trie building is one time cost. After trie is built then your string can be easily compared letter by letter. if at any point you encounter a leaf in the trie you can assume you found a word, add this to a list & move on with your traversal. Do the traversal till you have reached the end of your string. The list is output.
Time Complexity for search - O(word_length).
Space Complexity - O(charsize * word_length * no_words). Size of your dictionary.
Approach 2 - I have heard of Suffix Trees, never used them but it might be useful here.
Approach 3 - is more pedantic & a lousy alternative. you have already suggested this.
You could try the other way around. Run through the dict is check for sub-string match. Here I am assuming the keys in dict are the words of the english dictionary /usr/share/dict/words. So psuedo code looks something like this -
(list) splitIntoWords(String str, dict d)
{
words = []
for (word in d)
{
if word in str
words.append(word);
}
return words;
}
Complexity - O(n) running through entire dict + O(1) for substring match.
Space - worst case O(n) if len(words) == len(dict)
As others have pointed out, this does require backtracking.