-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path966.cpp
More file actions
59 lines (50 loc) · 1.77 KB
/
Copy path966.cpp
File metadata and controls
59 lines (50 loc) · 1.77 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Solution {
public:
vector<string> spellchecker(vector<string> &wordlist, vector<string> &queries) {
vector<string> ret;
unordered_set<string> dict;
unordered_map<string, int> lowerCaseDict;
unordered_map<string, int> vowelDict;
for (int i = 0; i < wordlist.size(); ++i) {
string word = wordlist[i];
dict.insert(word);
ToLower(word);
if(lowerCaseDict.find(word) == lowerCaseDict.end())
lowerCaseDict[word] = i;
for (int i = 0; i < word.size(); ++i) {
if (word[i] == 'a' || word[i] == 'e' || word[i] == 'i' || word[i] == 'o' ||
word[i] == 'u')
word[i] = '#';
}
if(vowelDict.find(word) == vowelDict.end())
vowelDict[word] = i;
}
for (auto q:queries) {
if (dict.find(q) != dict.end()) {
ret.push_back(q);
continue;
}
string temp = q;
ToLower(temp);
if (lowerCaseDict.find(temp) != lowerCaseDict.end()) {
ret.push_back(wordlist[lowerCaseDict[temp]]);
continue;
}
for (int i = 0; i < q.size(); ++i) {
if (temp[i] == 'a' || temp[i] == 'e' || temp[i] == 'i' || temp[i] == 'o' ||
temp[i] == 'u')
temp[i] = '#';
}
if (vowelDict.find(temp) != vowelDict.end()) {
ret.push_back(wordlist[vowelDict[temp]]);
continue;
}
ret.push_back("");
}
return ret;
}
void ToLower(string &str) {
for (int i = 0; i < str.size(); ++i)
str[i] = tolower(str[i]);
}
};