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:
wordlist = ["yellow"], query = "YellOw": correct = "yellow"wordlist = ["Yellow"], query = "yellow": correct = "Yellow"wordlist = ["yellow"], query = "yellow": correct = "yellow"('a', 'e', 'i', 'o', 'u') of the query word with any vowel individually, it matches a word in the wordlist (case-insensitive), then the query word is returned with the same case as the match in the wordlist.
wordlist = ["YellOw"], query = "yollow": correct = "YellOw"wordlist = ["YellOw"], query = "yeellow": correct = "" (no match)wordlist = ["YellOw"], query = "yllw": correct = "" (no match)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:
1 <= wordlist.length, queries.length <= 50001 <= wordlist[i].length, queries[i].length <= 7wordlist[i] and queries[i] consist only of only English letters.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;
}
}class Solution {
public String[] spellchecker(String[] wordlist, String[] queries) {
Set<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u'));
Set<String> exactMatch = new HashSet<>();
Map<String, String> mapCapitalize = new HashMap<>();
Map<String, String> mapError = new HashMap<>();
for (String w : wordlist) {
exactMatch.add(w);
String wl = w.toLowerCase();
mapCapitalize.putIfAbsent(wl, w);
String we = wl.chars().mapToObj(c -> vowels.contains((char) c) ? "*" : String.valueOf((char) c))
.collect(Collectors.joining());
mapError.putIfAbsent(we, w);
}
String[] ans = new String[queries.length];
int j = 0;
for (String q : queries) {
if (exactMatch.contains(q)) {
ans[j++] = q;
continue;
}
String ql = q.toLowerCase();
if (mapCapitalize.containsKey(ql)) {
ans[j++] = mapCapitalize.get(ql);
continue;
}
String qe = ql.chars().mapToObj(c -> vowels.contains((char) c) ? "*" : String.valueOf((char) c))
.collect(Collectors.joining());
if (mapError.containsKey(qe)) {
ans[j++] = mapError.get(qe);
continue;
}
ans[j++] = "";
}
return ans;
}
}
class Solution {
public String[] spellchecker(String[] wordlist, String[] queries) {
Set<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u'));
Set<String> exactMatch = new HashSet<>();
Map<String, String> mapCapitalize = new HashMap<>();
Map<String, String> mapError = new HashMap<>();
for (String w : wordlist) {
exactMatch.add(w);
String wl = w.toLowerCase();
mapCapitalize.putIfAbsent(wl, w);
String we = wl.chars().mapToObj(c -> vowels.contains((char) c) ? "*" : String.valueOf((char) c))
.collect(Collectors.joining());
mapError.putIfAbsent(we, w);
}
String[] ans = new String[queries.length];
int j = 0;
for (String q : queries) {
if (exactMatch.contains(q)) {
ans[j++] = q;
continue;
}
String ql = q.toLowerCase();
if (mapCapitalize.containsKey(ql)) {
ans[j++] = mapCapitalize.get(ql);
continue;
}
String qe = ql.chars().mapToObj(c -> vowels.contains((char) c) ? "*" : String.valueOf((char) c))
.collect(Collectors.joining());
if (mapError.containsKey(qe)) {
ans[j++] = mapError.get(qe);
continue;
}
ans[j++] = "";
}
return ans;
}
}