aboutsummaryrefslogtreecommitdiff
path: root/vowel_spellchecker.py
diff options
context:
space:
mode:
Diffstat (limited to 'vowel_spellchecker.py')
-rw-r--r--vowel_spellchecker.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/vowel_spellchecker.py b/vowel_spellchecker.py
new file mode 100644
index 0000000..ca1ad43
--- /dev/null
+++ b/vowel_spellchecker.py
@@ -0,0 +1,58 @@
+# 966. Vowel Spellchecker
+
+"""
+given a 'wordlist', we want to implement a spellchecker that converts a query
+word into a correct word. for a given 'query', the spell checker handles two
+categories of spelling mistakes: capitalisation and vowel errors. given some
+'queries', return a list of words 'answer' where 'answer[i]' is the correct
+word for 'query = queries[i]'
+"""
+
+
+class Solution(object):
+ def spellchecker(self, wordlist, queries):
+ """
+ :type wordlist: List[str]
+ :type queries: List[str]
+ :rtype: List[str]
+ """
+
+ def mask(s):
+ return "".join("*" if c in "aeiou" else c for c in s.lower())
+
+ hash1 = set(wordlist)
+ hash2 = {s.lower(): s for s in wordlist[::-1]}
+ hash3 = {mask(s): s for s in wordlist[::-1]}
+
+ def solve(query):
+ if query in hash1:
+ return query
+ if query.lower() in hash2:
+ return hash2[query.lower()]
+ if mask(query) in hash3:
+ return hash3[mask(query)]
+ return ""
+
+ return [solve(q) for q in queries]
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(
+ obj.spellchecker(
+ wordlist=["KiTe", "kite", "hare", "Hare"],
+ queries=[
+ "kite",
+ "Kite",
+ "KiTe",
+ "Hare",
+ "HARE",
+ "Hear",
+ "hear",
+ "keti",
+ "keet",
+ "keto",
+ ],
+ )
+ )
+ print(obj.spellchecker(wordlist=["yellow"], queries=["YellOw"]))