commit 24d08f726dc86d5a07c392b7fe416d67867e706e
parent 4d48c46926b5868ade4f9837d24f4409190fb0ca
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date: Thu, 30 May 2024 14:23:26 +0200
1 Random Problem
Diffstat:
2 files changed, 43 insertions(+), 0 deletions(-)
diff --git a/Problems/0966.cpp b/Problems/0966.cpp
@@ -0,0 +1,42 @@
+class Solution {
+ static bool isvowel(const char c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; }
+
+ static string devow(string word) {
+ for (char &c : word)
+ if (isvowel(c)) c = '_';
+ return word;
+ }
+
+ static string tolower(string word) {
+ for (char &c : word)
+ c = std::tolower(c);
+ return word;
+ }
+
+ public:
+ vector<string> spellchecker(const vector<string> &wordlist, vector<string> &queries) const {
+ unordered_set<string> words(begin(wordlist), end(wordlist));
+ unordered_map<string, string> caps, vows;
+
+ for (const auto &word : wordlist) {
+ const string low = tolower(word);
+ caps.emplace(low, word);
+ vows.emplace(devow(low), word);
+ }
+
+ for (auto &word : queries) {
+ if (words.count(word)) continue;
+ const string low = tolower(word);
+
+ const auto it = caps.find(low);
+ if (it != caps.end()) {
+ word = it->second;
+ continue;
+ }
+
+ word = vows[devow(low)];
+ }
+
+ return queries;
+ }
+};
diff --git a/README.md b/README.md
@@ -585,6 +585,7 @@ for solving problems.
| 0958 | Medium | [Check Completeness of a Binary Tree](Problems/0958.cpp) |
| 0959 | Medium | [Regions Cut By Slashes](Problems/0959.cpp) |
| 0965 | Easy | [Univalued Binary Tree](Problems/0965.cpp) |
+| 0966 | Medium | [Vowel Spellchecker](Problems/0966.cpp) |
| 0967 | Medium | [Numbers With Same Consecutive Differences](Problems/0967.cpp) |
| 0969 | Medium | [Pancake Sorting](Problems/0969.cpp) |
| 0973 | Medium | [K Closest Points to Origin](Problems/0973.cpp) |