LeetCode Entry

966. Vowel Spellchecker

14.09.2025 medium 2025 kotlin rust

Spellcheck with priority: original, case, vowels

966. Vowel Spellchecker medium blog post substack youtube

1.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1112

Problem TLDR

Spellcheck with priority: original, case, vowels #medium #regex

Intuition

Understand the priority:

  1. Exact match
  2. Ignore-case match
  3. Vowel-as-wildcard match

Approach

  • do full-search for vowels (7 symbols max, 5 wovels = 7^5) or precompute a wildcards

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(n)\)

Code


// 73ms
    fun spellchecker(wl: Array<String>, q: Array<String>): List<String> {
        val orig = wl.groupBy { it }; val lower = wl.groupBy { it.lowercase() }
        val vowel = wl.groupBy { Regex("[eiou]").replace(it.lowercase(), "a")}
        return q.map { q ->
            orig[q]?.first() ?: lower[q.lowercase()]?.first() ?:
            vowel[Regex("[eiou]").replace(q.lowercase(), "a")]?.first() ?: ""
        }
    }


// 13ms
    pub fn spellchecker(w: Vec<String>, q: Vec<String>) -> Vec<String> {
        use itertools::Itertools;
        let exact: HashMap<_,_> = w.iter().map(|s| (s.as_str(), s.as_str())).collect();
        let lower: HashMap<_,_> = w.iter().rev().map(|s| (s.to_lowercase(), s.as_str())).collect();
        let vowel: HashMap<_,_> = w.iter().rev().map(|s| (s.to_lowercase().replace(|c|"eiou".contains(c),"a"), s.as_str())).collect();
        q.iter().map(|s| {
            exact.get(s.as_str()).copied()
            .or(lower.get(&s.to_lowercase()).copied())
            .or(vowel.get(&s.to_lowercase().replace(|c|"eiou".contains(c),"a")).copied())
            .unwrap_or("").into()
        }).collect()
    }