Vowel Spellchecker

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"]
 

Note:


Solution:

class Solution {
    public String[] spellchecker(String[] wordlist, String[] queries) {
        int n = queries.length;
        String[] results = new String[n];
        Set<String> set = new HashSet();
        Map<String, String> lower = new HashMap();
        Map<String, String> vowel = new HashMap();
        for (String word : wordlist) {
            set.add(word);
            String l = word.toLowerCase();
            lower.putIfAbsent(l, word);
            vowel.putIfAbsent(l.replaceAll("[aeiou]", "#"), word);
        }
        for (int i = 0; i < n; i ++) {
            String query = queries[i];
            String lowerCase = query.toLowerCase();
            if (set.contains(query)) {
                results[i] = query;
            } else if (lower.containsKey(query.toLowerCase())) {
                results[i] = lower.get(query.toLowerCase());
            } else if (vowel.containsKey(lowerCase.replaceAll("[aeiou]", "#"))) {
                results[i] = vowel.get(lowerCase.replaceAll("[aeiou]", "#"));
            } else {
                results[i] = "";
            }

        }
        return results;
    }
}