forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
84 lines (68 loc) · 2.12 KB
/
main.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/// Source : https://leetcode.com/problems/vowel-spellchecker/
/// Author : liuyubobobo
/// Time : 2019-03-17
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std;
/// Using HashSet and HashMap
/// Time Complexity: O(n + q)
/// Space Complexity: O(n)
class Solution {
private:
const unordered_set<char> vowels = {'a', 'e', 'i', 'o', 'u'};
public:
vector<string> spellchecker(vector<string>& wordlist, vector<string>& queries) {
unordered_set<string> set;
unordered_map<string, vector<string>> map1; // all_lower -> origin
unordered_map<string, vector<string>> map2; // replace vowel -> origin
for(const string& word: wordlist){
set.insert(word);
string lower_word = to_lower(word);
map1[lower_word].push_back(word);
string novowel_word = replace_vowel(word);
map2[novowel_word].push_back(word);
}
vector<string> res;
for(const string& query: queries){
bool ok = false;
if(set.count(query)){
res.push_back(query);
ok = true;
}
else{
string lower_query = to_lower(query);
if(map1.count(lower_query)){
res.push_back(map1[lower_query][0]);
ok = true;
}
else{
string novowel_query = replace_vowel(query);
if(map2.count(novowel_query)){
res.push_back(map2[novowel_query][0]);
ok = true;
}
}
}
if(!ok) res.push_back("");
}
return res;
}
private:
string to_lower(const string& word){
string res = "";
for(char c: word) res += tolower(c);
return res;
}
string replace_vowel(const string& word){
string res = "";
for(char c: word)
if(vowels.count(tolower(c))) res += "#";
else res += tolower(c);
return res;
}
};
int main() {
return 0;
}