website/content.en/ChapterFour/0900~0999/0966.Vowel-Spellchecker.md
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"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"]
Note:
1 <= wordlist.length <= 50001 <= queries.length <= 50001 <= wordlist[i].length <= 71 <= queries[i].length <= 7wordlist and queries consist only of english letters.Given a word list wordlist, we want to implement a spellchecker that converts a query word into the correct word.
For a given query word query, the spellchecker will handle two types of spelling mistakes:
In addition, the spellchecker also operates according to the following precedence rules:
Given some queries queries, return a word list answer, where answer[i] is the correct word obtained from query query = queries[i].
map is needed to solve it. According to the problem statement, there are 3 cases: the query string matches exactly; the query string differs only in capitalization; the query string has a vowel error. For the first case, use a map key to match directly. For the second case, use a map to convert the lowercase form of a word into the original word with the correct capitalization. For the third case, use a map to convert the lowercase form of a word with vowels ignored into the original correct form. Finally, just pay attention to the 4 precedence rules given at the end of the problem.package leetcode
import "strings"
func spellchecker(wordlist []string, queries []string) []string {
wordsPerfect, wordsCap, wordsVowel := map[string]bool{}, map[string]string{}, map[string]string{}
for _, word := range wordlist {
wordsPerfect[word] = true
wordLow := strings.ToLower(word)
if _, ok := wordsCap[wordLow]; !ok {
wordsCap[wordLow] = word
}
wordLowVowel := devowel(wordLow)
if _, ok := wordsVowel[wordLowVowel]; !ok {
wordsVowel[wordLowVowel] = word
}
}
res, index := make([]string, len(queries)), 0
for _, query := range queries {
if _, ok := wordsPerfect[query]; ok {
res[index] = query
index++
continue
}
queryL := strings.ToLower(query)
if v, ok := wordsCap[queryL]; ok {
res[index] = v
index++
continue
}
queryLV := devowel(queryL)
if v, ok := wordsVowel[queryLV]; ok {
res[index] = v
index++
continue
}
res[index] = ""
index++
}
return res
}
func devowel(word string) string {
runes := []rune(word)
for k, c := range runes {
if c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' {
runes[k] = '*'
}
}
return string(runes)
}