设计一个方法,找出任意指定单词在一本书中的出现频率。
你的实现应该支持如下操作:
WordsFrequency(book)构造函数,参数为字符串数组构成的一本书
get(word)查询指定单词在书中出现的频率
示例:
WordsFrequency wordsFrequency = new WordsFrequency({"i", "have", "an", "apple", "he", "have", "a", "pen"});
wordsFrequency.get("you"); //返回0,"you"没有出现过
wordsFrequency.get("have"); //返回2,"have"出现2次
wordsFrequency.get("an"); //返回1
wordsFrequency.get("apple"); //返回1
wordsFrequency.get("pen"); //返回1
提示:
book[i]中只包含小写字母
1 <= book.length <= 100000
1 <= book[i].length <= 10
get函数的调用次数不会超过100000
AC代码
const int TRIE_NODE_SIZE = 26;
// 字典树节点
struct TrieNode {
TrieNode* next[TRIE_NODE_SIZE];
bool isEnd;
int times; // 记录出现次数
TrieNode()
{
for (int i = 0; i < TRIE_NODE_SIZE; ++i) {
next[i] = nullptr;
isEnd = false;
times = 0;
}
}
};
// 字典树
class Trie {
public:
Trie() {
root = new TrieNode();
}
void insert(string word) {
TrieNode* node = root;
for (const auto& ch : word) {
if (node->next[ch - 'a'] == nullptr) {
node->next[ch - 'a'] = new TrieNode();
}
node = node->next[ch - 'a'];
}
node->isEnd = true;
node->times++;
}
bool search(string word) {
TrieNode* node = root;
for (const auto& ch : word) {
if (node->next[ch - 'a'] != nullptr) {
node = node->next[ch - 'a'];
} else {
return false;
}
}
return node->isEnd;
}
bool startsWith(string prefix) {
TrieNode* node = root;
for (const auto& ch : prefix) {
if (node->next[ch - 'a'] != nullptr) {
node = node->next[ch - 'a'];
} else {
return false;
}
}
return true;
}
// 获取出现次数
int getTimes(string word) {
TrieNode* node = root;
for (const auto& ch : word) {
if (node->next[ch - 'a'] != nullptr) {
node = node->next[ch - 'a'];
} else {
return 0;
}
}
return node->times;
}
public:
TrieNode* root; // 根节点
};
class WordsFrequency {
public:
WordsFrequency(vector<string>& book) {
trie = new Trie();
for (const auto& word : book) {
trie->insert(word);
}
}
int get(string word) {
return trie->getTimes(word);
}
public:
Trie* trie;
};
/**
* Your WordsFrequency object will be instantiated and called as such:
* WordsFrequency* obj = new WordsFrequency(book);
* int param_1 = obj->get(word);
*/