-
Notifications
You must be signed in to change notification settings - Fork 481
/
0966.cpp
40 lines (38 loc) · 1.24 KB
/
0966.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
#include <vector>
#include <string>
#include <unordered_map>
#include <algorithm>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
vector<string> spellchecker(vector<string>& wordlist, vector<string>& queries)
{
unordered_map<string, string> words;
for (auto w : wordlist)
{
words.insert({ w, w }), words.insert({ lowerKey(w), w }), words.insert({ vowelKey(w), w });
}
vector<string> res;
for (auto q : queries)
{
auto it = words.find(q);
if (it == words.end()) it = words.find(lowerKey(q));
if (it == words.end()) it = words.find(vowelKey(q));
if (it != words.end()) res.push_back(it->second);
else res.push_back("");
}
return res;
}
private:
string lowerKey(string &s)
{
return accumulate(begin(s), end(s), string("_"), [](string k, char c) { return k + (char)tolower(c); });
}
string vowelKey(string &s)
{
return accumulate(begin(s), end(s), string(""), [](string k, char c) { return k +
(char)(string("aeiou").find(tolower(c)) != string::npos ? '*' : tolower(c)); });
}
};