LeetCode 966: Vowel Spellchecker Solution

Master LeetCode problem 966 (Vowel Spellchecker), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

966. Vowel Spellchecker

Problem Explanation

Explanation:

  • We will preprocess the wordlist to store the original word, lowercase word, and word with vowels replaced by a placeholder.
  • For each query, we will first check if it is an exact match, then a match with the same case, and finally a match with vowels replaced.
  • We will return the correct word based on the precedence rules mentioned in the problem.

Time Complexity: Let N be the total number of characters in the wordlist and queries combined.

  • Preprocessing: O(N) to preprocess the wordlist.
  • Query processing: O(N) for processing each query.
  • Overall: O(N)

Space Complexity: O(N) for storing the preprocessed wordlist.

:

Solution Code

class Solution {
    public String[] spellchecker(String[] wordlist, String[] queries) {
        Set<String> wordSet = new HashSet<>();
        Map<String, String> lowerMap = new HashMap<>();
        Map<String, String> vowelMap = new HashMap<>();
        
        for (String word : wordlist) {
            wordSet.add(word);
            
            String lower = word.toLowerCase();
            lowerMap.putIfAbsent(lower, word);
            
            String vowel = lower.replaceAll("[aeiou]", "_");
            vowelMap.putIfAbsent(vowel, word);
        }
        
        String[] result = new String[queries.length];
        for (int i = 0; i < queries.length; i++) {
            String query = queries[i];
            if (wordSet.contains(query)) {
                result[i] = query;
            } else {
                String lower = query.toLowerCase();
                if (lowerMap.containsKey(lower)) {
                    result[i] = lowerMap.get(lower);
                } else {
                    String vowel = lower.replaceAll("[aeiou]", "_");
                    result[i] = vowelMap.getOrDefault(vowel, "");
                }
            }
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 966 (Vowel Spellchecker)?

This page provides optimized solutions for LeetCode problem 966 (Vowel Spellchecker) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 966 (Vowel Spellchecker)?

The time complexity for LeetCode 966 (Vowel Spellchecker) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 966 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 966 in Java, C++, or Python.

Back to LeetCode Solutions