|
1 | 1 | //go:build ignore |
2 | 2 | #include "cpp/common/Solution.h" |
3 | | - |
| 3 | +#include <algorithm> |
4 | 4 |
|
5 | 5 | using namespace std; |
6 | 6 | using json = nlohmann::json; |
7 | 7 |
|
| 8 | +constexpr string VOWELS = "aeiou"; |
| 9 | + |
8 | 10 | class Solution { |
9 | 11 | public: |
10 | | - vector<string> spellchecker(vector<string>& wordlist, vector<string>& queries) { |
11 | | - |
| 12 | + vector<string> spellchecker(const vector<string> &wordlist, |
| 13 | + const vector<string> &queries) { |
| 14 | + unordered_set<string> origin; |
| 15 | + unordered_map<string, string> ignore_cases, ignore_vowels; |
| 16 | + |
| 17 | + auto strIgnoreVowels = [](string &w) -> void { |
| 18 | + for (auto &c : w) { |
| 19 | + if (VOWELS.find(c) != string::npos) { |
| 20 | + c = '*'; |
| 21 | + } |
| 22 | + } |
| 23 | + }; |
| 24 | + |
| 25 | + for (const auto &word : wordlist) { |
| 26 | + origin.insert(word); |
| 27 | + string lower = word; |
| 28 | + std::transform(word.begin(), word.end(), lower.begin(), ::tolower); |
| 29 | + if (ignore_cases.find(lower) == ignore_cases.end()) { |
| 30 | + ignore_cases[lower] = word; |
| 31 | + } |
| 32 | + strIgnoreVowels(lower); |
| 33 | + if (ignore_vowels.find(lower) == ignore_vowels.end()) { |
| 34 | + ignore_vowels[lower] = word; |
| 35 | + } |
12 | 36 | } |
| 37 | + |
| 38 | + vector<string> ans; |
| 39 | + for (const auto &query : queries) { |
| 40 | + if (origin.contains(query)) { |
| 41 | + ans.emplace_back(query); |
| 42 | + continue; |
| 43 | + } |
| 44 | + string lower = query; |
| 45 | + std::transform(query.begin(), query.end(), lower.begin(), ::tolower); |
| 46 | + auto it = ignore_cases.find(lower); |
| 47 | + if (it != ignore_cases.end()) { |
| 48 | + ans.emplace_back(it->second); |
| 49 | + continue; |
| 50 | + } |
| 51 | + strIgnoreVowels(lower); |
| 52 | + it = ignore_vowels.find(lower); |
| 53 | + if (it != ignore_vowels.end()) { |
| 54 | + ans.emplace_back(it->second); |
| 55 | + continue; |
| 56 | + } |
| 57 | + ans.emplace_back(""); |
| 58 | + } |
| 59 | + return ans; |
| 60 | + } |
13 | 61 | }; |
14 | 62 |
|
15 | 63 | json leetcode::qubh::Solve(string input_json_values) { |
16 | | - vector<string> inputArray; |
17 | | - size_t pos = input_json_values.find('\n'); |
18 | | - while (pos != string::npos) { |
19 | | - inputArray.push_back(input_json_values.substr(0, pos)); |
20 | | - input_json_values = input_json_values.substr(pos + 1); |
21 | | - pos = input_json_values.find('\n'); |
22 | | - } |
23 | | - inputArray.push_back(input_json_values); |
24 | | - |
25 | | - Solution solution; |
26 | | - vector<string> wordlist = json::parse(inputArray.at(0)); |
27 | | - vector<string> queries = json::parse(inputArray.at(1)); |
28 | | - return solution.spellchecker(wordlist, queries); |
| 64 | + vector<string> inputArray; |
| 65 | + size_t pos = input_json_values.find('\n'); |
| 66 | + while (pos != string::npos) { |
| 67 | + inputArray.push_back(input_json_values.substr(0, pos)); |
| 68 | + input_json_values = input_json_values.substr(pos + 1); |
| 69 | + pos = input_json_values.find('\n'); |
| 70 | + } |
| 71 | + inputArray.push_back(input_json_values); |
| 72 | + |
| 73 | + Solution solution; |
| 74 | + vector<string> wordlist = json::parse(inputArray.at(0)); |
| 75 | + vector<string> queries = json::parse(inputArray.at(1)); |
| 76 | + return solution.spellchecker(wordlist, queries); |
29 | 77 | } |
0 commit comments