Skip to content

Commit b1a237c

Browse files
committed
Runtime: 84 ms (Top 92.95%) | Memory: 32.7 MB (Top 97.44%)
1 parent 42860cb commit b1a237c

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,69 @@
1+
// Runtime: 84 ms (Top 92.95%) | Memory: 32.7 MB (Top 97.44%)
12
class Solution {
23
public:
34
vector<string> spellchecker(vector<string>& wordlist, vector<string>& queries) {
45
unordered_map<string, vector<int>> umap;
5-
6+
67
// step 1: add info in umap;
78
for(int curr = 0; curr < wordlist.size(); curr++){
89
// case 1: add same;
910
umap[wordlist[curr]].push_back({curr});
1011
// notice that the lowercase may appear;
11-
12+
1213
// case 2: add lowercase;
1314
string tmp = wordlist[curr];
1415
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+
1819
// case 3: add vowel errors;
1920
// convert aeiou to _;
2021
for(int c_index = 0; c_index < tmp.size(); c_index++){
2122
char c = tmp[c_index];
2223
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') tmp[c_index] = '_';
2324
}
24-
25+
2526
if(umap.find(tmp) == umap.end()) umap[tmp].push_back({curr});
2627
}
27-
28+
2829
// step 2: convert queries;
2930
for(int curr = 0; curr < queries.size(); curr++){
3031
string tmp = queries[curr];
3132
transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
32-
33+
3334
// case 1: check same;
3435
if(umap.find(queries[curr]) != umap.end()){
3536
queries[curr] = (umap[queries[curr]].size() == 1) ? wordlist[umap[queries[curr]][0]] : wordlist[umap[queries[curr]][1]];
3637
continue;
3738
}
38-
39+
3940
// case 2: check lowercase;
4041
if(umap.find(tmp) != umap.end() && tmp != queries[curr]){
4142
queries[curr] = wordlist[umap[tmp][0]];
4243
continue;
4344
}
44-
45+
4546
// case 3: check vowel errors;
4647
// convert aeiou to _;
4748
for(int c_index = 0; c_index < tmp.size(); c_index++){
4849
char c = tmp[c_index];
4950
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') tmp[c_index] = '_';
5051
}
51-
52+
5253
if(umap.find(tmp) != umap.end()){
5354
queries[curr] = wordlist[umap[tmp][0]];
5455
continue;
5556
}
56-
57+
5758
// case 4: not found;
5859
queries[curr] = "";
5960
}
60-
61+
6162
return queries;
6263
}
6364
};
6465

6566
// 1. When the query exactly matches a word in the wordlist (case-sensitive), you should return the same word back.
6667
// 2. When the query matches a word up to capitlization, you should return the first such match in the wordlist.
6768
// 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

Comments
 (0)