Skip to content

Commit

Permalink
Update prefix-and-suffix-search.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Dec 12, 2017
1 parent 69dcd2c commit b0596ac
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions C++/prefix-and-suffix-search.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

// Time: ctor: O(w * l^2), w is the number of words, l is the word length on average
// search: O(p + s) , p is the length of the prefix, s is the length of the suffix,
// Space: O(t), t is the number of trie nodes
Expand Down Expand Up @@ -44,9 +45,9 @@ class WordFilter {
public:
WordFilter(vector<string> words) {
for (int i = 0; i < words.size(); ++i) {
auto word = words[i] + SEPARATOR + words[i];
for (int j = 0; j < word.length(); ++j) {
trie_.insert(word.substr(j), i);
for (int j = 0; j <= words[i].length(); ++j) {
const auto& word = words[i].substr(j) + SEPARATOR + words[i];
trie_.insert(word, i);
}
}
}
Expand Down

0 comments on commit b0596ac

Please sign in to comment.