Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
vowel-spellchecker.cpp 1.27 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2018-12-30 15:17 +08:00 . Update vowel-spellchecker.cpp
// Time: O(n)
// Space: O(w)
class Solution {
public:
vector<string> spellchecker(vector<string>& wordlist, vector<string>& queries) {
unordered_set<string> words(wordlist.cbegin(), wordlist.cend());
unordered_map<string, string> caps, vows;
for (const auto& word : wordlist) {
const auto& lower = tolow(word);
caps.emplace(lower, word);
vows.emplace(todev(lower), word);
}
for (auto& query : queries) {
if (words.count(query)) {
continue;
}
const auto& lower = tolow(query);
const auto& devow = todev(lower);
if (caps.count(lower)) {
query = caps[lower];
} else if (vows.count(devow)) {
query = vows[devow];
} else {
query = "";
}
}
return queries;
}
private:
string tolow(string word) {
for (auto& c: word) {
c = tolower(c);
}
return word;
}
string todev(string word) {
static unordered_set<char> vowels{'a', 'e', 'i', 'o', 'u'};
for (auto& c: word) {
if (vowels.count(c)) {
c = '*';
}
}
return word;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助