0966.cpp (1164B)
1 class Solution { 2 static bool isvowel(const char c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; } 3 4 static string devow(string word) { 5 for (char &c : word) 6 if (isvowel(c)) c = '_'; 7 return word; 8 } 9 10 static string tolower(string word) { 11 for (char &c : word) 12 c = std::tolower(c); 13 return word; 14 } 15 16 public: 17 vector<string> spellchecker(const vector<string> &wordlist, vector<string> &queries) const { 18 unordered_set<string> words(begin(wordlist), end(wordlist)); 19 unordered_map<string, string> caps, vows; 20 21 for (const auto &word : wordlist) { 22 const string low = tolower(word); 23 caps.emplace(low, word); 24 vows.emplace(devow(low), word); 25 } 26 27 for (auto &word : queries) { 28 if (words.count(word)) continue; 29 const string low = tolower(word); 30 31 const auto it = caps.find(low); 32 if (it != caps.end()) { 33 word = it->second; 34 continue; 35 } 36 37 word = vows[devow(low)]; 38 } 39 40 return queries; 41 } 42 };