题目:
设计一个包含一些单词的特殊词典,并能够通过前缀和后缀来检索单词。
实现 WordFilter 类:
WordFilter(string[] words) 使用词典中的单词 words 初始化对象。
f(string pref, string suff) 返回词典中具有前缀 prefix 和后缀 suff 的单词的下标。如果存在不止一个满足要求的下标,返回其中 最大的下标 。如果不存在这样的单词,返回 -1 。
示例:
输入
["WordFilter", "f"]
[[["apple"]], ["a", "e"]]
输出
[null, 0]
解释
WordFilter wordFilter = new WordFilter(["apple"]);
wordFilter.f("a", "e"); // 返回 0 ,因为下标为 0 的单词:前缀 prefix = "a" 且 后缀 suff = "e" 。
提示:
1 <= words.length <= 104
1 <= words[i].length <= 7
1 <= pref.length, suff.length <= 7
words[i]、pref 和 suff 仅由小写英文字母组成
最多对函数 f 执行 104 次调用
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/prefix-and-suffix-search
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
class Trie
{
public:
bool isWord;
int weight;
unordered_map<char, Trie*> child;
Trie ()
{
this->isWord = false;
this->weight = -1;
}
void insert(string word)
{
Trie * rt = this;
for (char c: word)
{
if (rt->child.count(c) == 0)
rt->child[c] = new Trie();
rt = rt->child[c];
}
rt->isWord = true;
}
bool search(string word)
{
Trie* rt = this;
for (char c: word)
{
if (rt->child.count(c) == 0)
return false;
rt = rt->child[c];
}
return rt->isWord == true;
}
bool startsWith(string word)
{
Trie* rt = this;
for (char c: word)
{
if (rt->child.count(c) == 0)
return false;
rt = rt->child[c];
}
return true;
}
};
class WordFilter
{
public:
Trie * root;
WordFilter(vector<string>& words)
{
root = new Trie();
for (int weight = 0; weight < words.size(); weight ++)
{
string word = words[weight]; //apple
string tmp = word + '#' + word; //apple#apple
for (int i = 0; i < word.size() + 1; i ++)
{
Trie* cur = root;
for (int j = i; j < tmp.size(); j ++) //如ple#apple
{
if (cur->child.count(tmp[j]) == 0)
cur->child[tmp[j]] = new Trie();
cur = cur->child[tmp[j]];
cur->weight = weight;
}
}
}
}
int f(string prefix, string suffix)
{
Trie* rt = root;
string tmp = suffix + '#' + prefix;
for (char c: tmp)
{
if (rt->child.count(c) == 0)
return -1;
rt = rt->child[c];
}
return rt->weight;
}
};
/**
* Your WordFilter object will be instantiated and called as such:
* WordFilter* obj = new WordFilter(words);
* int param_1 = obj->f(pref,suff);
*/