-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0966.cpp
More file actions
42 lines (34 loc) · 1.14 KB
/
Copy path0966.cpp
File metadata and controls
42 lines (34 loc) · 1.14 KB
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
class Solution {
static bool isvowel(const char c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; }
static string devow(string word) {
for (char &c : word)
if (isvowel(c)) c = '_';
return word;
}
static string tolower(string word) {
for (char &c : word)
c = std::tolower(c);
return word;
}
public:
vector<string> spellchecker(const vector<string> &wordlist, vector<string> &queries) const {
unordered_set<string> words(begin(wordlist), end(wordlist));
unordered_map<string, string> caps, vows;
for (const auto &word : wordlist) {
const string low = tolower(word);
caps.emplace(low, word);
vows.emplace(devow(low), word);
}
for (auto &word : queries) {
if (words.count(word)) continue;
const string low = tolower(word);
const auto it = caps.find(low);
if (it != caps.end()) {
word = it->second;
continue;
}
word = vows[devow(low)];
}
return queries;
}
};