1
+ // Runtime: 84 ms (Top 92.95%) | Memory: 32.7 MB (Top 97.44%)
1
2
class Solution {
2
3
public:
3
4
vector<string> spellchecker (vector<string>& wordlist, vector<string>& queries) {
4
5
unordered_map<string, vector<int >> umap;
5
-
6
+
6
7
// step 1: add info in umap;
7
8
for (int curr = 0 ; curr < wordlist.size (); curr++){
8
9
// case 1: add same;
9
10
umap[wordlist[curr]].push_back ({curr});
10
11
// notice that the lowercase may appear;
11
-
12
+
12
13
// case 2: add lowercase;
13
14
string tmp = wordlist[curr];
14
15
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
-
16
+
17
+ if (umap.find (tmp) == umap.end () && tmp != wordlist[curr]) umap[tmp].push_back ({curr});
18
+
18
19
// case 3: add vowel errors;
19
20
// convert aeiou to _;
20
21
for (int c_index = 0 ; c_index < tmp.size (); c_index++){
21
22
char c = tmp[c_index];
22
23
if (c == ' a' || c == ' e' || c == ' i' || c == ' o' || c == ' u' ) tmp[c_index] = ' _' ;
23
24
}
24
-
25
+
25
26
if (umap.find (tmp) == umap.end ()) umap[tmp].push_back ({curr});
26
27
}
27
-
28
+
28
29
// step 2: convert queries;
29
30
for (int curr = 0 ; curr < queries.size (); curr++){
30
31
string tmp = queries[curr];
31
32
transform (tmp.begin (), tmp.end (), tmp.begin (), ::tolower);
32
-
33
+
33
34
// case 1: check same;
34
35
if (umap.find (queries[curr]) != umap.end ()){
35
36
queries[curr] = (umap[queries[curr]].size () == 1 ) ? wordlist[umap[queries[curr]][0 ]] : wordlist[umap[queries[curr]][1 ]];
36
37
continue ;
37
38
}
38
-
39
+
39
40
// case 2: check lowercase;
40
41
if (umap.find (tmp) != umap.end () && tmp != queries[curr]){
41
42
queries[curr] = wordlist[umap[tmp][0 ]];
42
43
continue ;
43
44
}
44
-
45
+
45
46
// case 3: check vowel errors;
46
47
// convert aeiou to _;
47
48
for (int c_index = 0 ; c_index < tmp.size (); c_index++){
48
49
char c = tmp[c_index];
49
50
if (c == ' a' || c == ' e' || c == ' i' || c == ' o' || c == ' u' ) tmp[c_index] = ' _' ;
50
51
}
51
-
52
+
52
53
if (umap.find (tmp) != umap.end ()){
53
54
queries[curr] = wordlist[umap[tmp][0 ]];
54
55
continue ;
55
56
}
56
-
57
+
57
58
// case 4: not found;
58
59
queries[curr] = " " ;
59
60
}
60
-
61
+
61
62
return queries;
62
63
}
63
64
};
64
65
65
66
// 1. When the query exactly matches a word in the wordlist (case-sensitive), you should return the same word back.
66
67
// 2. When the query matches a word up to capitlization, you should return the first such match in the wordlist.
67
68
// 3. When the query matches a word up to vowel errors, you should return the first such match in the wordlist.
68
- // 4. If the query has no matches in the wordlist, you should return the empty string.
69
+ // 4. If the query has no matches in the wordlist, you should return the empty string.
0 commit comments