1
class Solution {
2
public:
3
vector<string> spellchecker(vector<string> &wordlist, vector<string> &queries) {
4
unordered_map<string, vector<int>> umap;
5

6
// step 1: add info in umap;
7
for (int curr = 0; curr < wordlist.size(); curr++) {
8
// case 1: add same;
9
umap[wordlist[curr]].push_back({curr});
10
// notice that the lowercase may appear;
11

12
// case 2: add lowercase;
13
string tmp = wordlist[curr];
14
transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
15

16
if (umap.find(tmp) == umap.end() && tmp != wordlist[curr]) umap[tmp].push_back({curr});
17

18
// case 3: add vowel errors;
19
// convert aeiou to _;
20
for (int c_index = 0; c_index < tmp.size(); c_index++) {
21
char c = tmp[c_index];
22
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') tmp[c_index] = '_';
23
}
24

25
if (umap.find(tmp) == umap.end()) umap[tmp].push_back({curr});
26
}
27

28
// step 2: convert queries;
29
for (int curr = 0; curr < queries.size(); curr++) {
30
string tmp = queries[curr];
31
transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
32

33
// case 1: check same;
34
if (umap.find(queries[curr]) != umap.end()) {
35
queries[curr] = (umap[queries[curr]].size() == 1) ? wordlist[umap[queries[curr]][0]]
36
: wordlist[umap[queries[curr]][1]];
37
continue;
38
}
39

40
// case 2: check lowercase;
41
if (umap.find(tmp) != umap.end() && tmp != queries[curr]) {
42
queries[curr] = wordlist[umap[tmp][0]];
43
continue;
44
}
45

46
// case 3: check vowel errors;
47
// convert aeiou to _;
48
for (int c_index = 0; c_index < tmp.size(); c_index++) {
49
char c = tmp[c_index];
50
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') tmp[c_index] = '_';
51
}
52

53
if (umap.find(tmp) != umap.end()) {
54
queries[curr] = wordlist[umap[tmp][0]];
55
continue;
56
}
57

58
// case 4: not found;
59
queries[curr] = "";
60
}
61

62
return queries;
63
}
64
};
65

66
// 1. When the query exactly matches a word in the wordlist (case-sensitive),
67
// you should return the same word back.
68
// 2. When the query matches a word up to capitlization, you should return the
69
// first such match in the wordlist.
70
// 3. When the query matches a word up to vowel errors, you should return the
71
// first such match in the wordlist.
72
// 4. If the query has no matches in the wordlist, you should return the empty
73
// string.

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0