LeetCode solutions

1006. Vowel Spellchecker

My accepted Python solution to LeetCode problem 1006, Vowel Spellchecker, running in 39ms.

  • Difficulty: Medium
  • Python
  • Runtime 39ms
  • Memory 19.6MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 39ms, memory 19.6MB, accepted 2026-01-02.

python
class Solution:
    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:
        # Exact match set
        word_set = set(wordlist)
        
        # Case-insensitive map (first match wins)
        cap_map = {}
        for word in wordlist:
            low = word.lower()
            if low not in cap_map:
                cap_map[low] = word
        
        # Vowel-insensitive map (replace vowels with *)
        def devowel(word):
            return ''.join('*' if c in 'aeiou' else c for c in word.lower())
        
        vowel_map = {}
        for word in wordlist:
            dv = devowel(word)
            if dv not in vowel_map:
                vowel_map[dv] = word
        
        result = []
        for query in queries:
            # Rule 1: Exact match
            if query in word_set:
                result.append(query)
            # Rule 2: Case-insensitive match
            elif query.lower() in cap_map:
                result.append(cap_map[query.lower()])
            # Rule 3: Vowel replacement match
            elif devowel(query) in vowel_map:
                result.append(vowel_map[devowel(query)])
            else:
                result.append("")
        
        return result

Source