leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0966.cpp (1164B)


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