diff options
author | Jack Sangdahl <[email protected]> | 2025-09-13 21:38:41 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-09-13 21:38:41 -0600 |
commit | 64b99fa5197737fab06e6e8465a203be985b07a8 (patch) | |
tree | f48437ea05d7e77b1f1b59b8bcaac015438a09aa /vowel_spellchecker.py | |
parent | 77d6e1dd41c644ed28fb61a3f19614965b43908c (diff) | |
download | leetcode-64b99fa5197737fab06e6e8465a203be985b07a8.tar.gz leetcode-64b99fa5197737fab06e6e8465a203be985b07a8.zip |
966. vowel spellchecker
Diffstat (limited to 'vowel_spellchecker.py')
-rw-r--r-- | vowel_spellchecker.py | 58 |
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"])) |