forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 14
/
vowel-spellchecker.cpp
49 lines (45 loc) · 1.27 KB
/
vowel-spellchecker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 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;
}
};