← All problems

966. Vowel Spellchecker

MediumOpen on LeetCodeProblem statement

Problem Statement

966. Vowel Spellchecker

Medium


Given a wordlist, we want to implement a spellchecker that converts a query word into a correct word.

For a given query word, the spell checker handles two categories of spelling mistakes:

In addition, the spell checker operates under the following precedence rules:

Given some queries, return a list of words answer, where answer[i] is the correct word for query = queries[i].

 

Example 1:

Input: wordlist = ["KiTe","kite","hare","Hare"], queries = ["kite","Kite","KiTe","Hare","HARE","Hear","hear","keti","keet","keto"]
Output: ["kite","KiTe","KiTe","Hare","hare","","","KiTe","","KiTe"]

Example 2:

Input: wordlist = ["yellow"], queries = ["YellOw"]
Output: ["yellow"]

 

Constraints:

Java — regex

Source file
class Solution {
    public String[] spellchecker(String[] wordlist, String[] queries) {
        Map<String, String> mapExact = new HashMap<>();
        Map<String, String> mapCapitalize = new HashMap<>();
        Map<String, String> mapError = new HashMap<>();
        int m = wordlist.length, n = queries.length;
        for (int i = m - 1; i >= 0; i--) {
            mapExact.put(wordlist[i], wordlist[i]);
            mapCapitalize.put(wordlist[i].toLowerCase(), wordlist[i]);
            mapError.put(wordlist[i].toLowerCase().replaceAll("[aeiou]", "*"), wordlist[i]);
        }
        String[] ans = new String[n];
        for (int i = 0; i < n; i++) {
            String q = queries[i];
            ans[i] = mapExact.getOrDefault(q,
                    mapCapitalize.getOrDefault(q.toLowerCase(),
                            mapError.getOrDefault(q.toLowerCase().replaceAll("[aeiou]", "*"),
                                    "")));
        }
        return ans;
    }
}