Reputation: 776
Requirement
Question: Check whether the character string contains keywords or sentences in list given
The question can be described as word filter on wikipedia, but I didn't find any algorithm on that page. The simplest way to fix this problem is iterator all the keywords or sentences and each time check whether the long text contains such substring. As we have a number of keywords, also considering the long text, the performance is very bad. It uses O(NL) time
Seems the better solution should be finished in O(L). Could anyone give some suggestion about this?
Upvotes: 2
Views: 1162
Reputation: 24647
There are several approaches to this problem having time complexity O(M + L), where L is length of the string and M is combined length of all patterns:
You can find details for all these algorithms (except Commentz-Walter algorithm) in this book: Algorithms on Strings, Trees and Sequences by Dan Gusfield.
Several different (simpler) approaches may be used if you can unambiguously extract separate words/sentences from input string.
Upvotes: 4