mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 09:23:19 +08:00
Add solution 966
This commit is contained in:
53
leetcode/0966.Vowel-Spellchecker/966. Vowel Spellchecker.go
Normal file
53
leetcode/0966.Vowel-Spellchecker/966. Vowel Spellchecker.go
Normal file
@ -0,0 +1,53 @@
|
||||
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)
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question966 struct {
|
||||
para966
|
||||
ans966
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para966 struct {
|
||||
wordlist []string
|
||||
queries []string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans966 struct {
|
||||
one []string
|
||||
}
|
||||
|
||||
func Test_Problem966(t *testing.T) {
|
||||
|
||||
qs := []question966{
|
||||
{
|
||||
para966{[]string{"KiTe", "kite", "hare", "Hare"}, []string{"kite", "Kite", "KiTe", "Hare", "HARE", "Hear", "hear", "keti", "keet", "keto"}},
|
||||
ans966{[]string{"kite", "KiTe", "KiTe", "Hare", "hare", "", "", "KiTe", "", "KiTe"}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 966------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans966, q.para966
|
||||
fmt.Printf("【input】:%v 【output】:%#v\n", p, spellchecker(p.wordlist, p.queries))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
127
leetcode/0966.Vowel-Spellchecker/README.md
Normal file
127
leetcode/0966.Vowel-Spellchecker/README.md
Normal file
@ -0,0 +1,127 @@
|
||||
# [966. Vowel Spellchecker](https://leetcode.com/problems/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:
|
||||
|
||||
- Capitalization: If the query matches a word in the wordlist (**case-insensitive**), then the query word is returned with the same case as the case in the wordlist.
|
||||
- Example: `wordlist = ["yellow"]`, `query = "YellOw"`: `correct = "yellow"`
|
||||
- Example: `wordlist = ["Yellow"]`, `query = "yellow"`: `correct = "Yellow"`
|
||||
- Example: `wordlist = ["yellow"]`, `query = "yellow"`: `correct = "yellow"`
|
||||
- Vowel Errors: If after replacing the vowels ('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.
|
||||
- Example: `wordlist = ["YellOw"]`, `query = "yollow"`: `correct = "YellOw"`
|
||||
- Example: `wordlist = ["YellOw"]`, `query = "yeellow"`: `correct = ""` (no match)
|
||||
- Example: `wordlist = ["YellOw"]`, `query = "yllw"`: `correct = ""` (no match)
|
||||
|
||||
In addition, the spell checker operates under the following precedence rules:
|
||||
|
||||
- When the query exactly matches a word in the wordlist (**case-sensitive**), you should return the same word back.
|
||||
- When the query matches a word up to capitlization, you should return the first such match in the wordlist.
|
||||
- When the query matches a word up to vowel errors, you should return the first such match in the wordlist.
|
||||
- If the query has no matches in the wordlist, you should return the empty string.
|
||||
|
||||
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 <= 5000`
|
||||
- `1 <= queries.length <= 5000`
|
||||
- `1 <= wordlist[i].length <= 7`
|
||||
- `1 <= queries[i].length <= 7`
|
||||
- All strings in `wordlist` and `queries` consist only of **english** letters.
|
||||
|
||||
## 题目大意
|
||||
|
||||
在给定单词列表 wordlist 的情况下,我们希望实现一个拼写检查器,将查询单词转换为正确的单词。
|
||||
|
||||
对于给定的查询单词 query,拼写检查器将会处理两类拼写错误:
|
||||
|
||||
- 大小写:如果查询匹配单词列表中的某个单词(不区分大小写),则返回的正确单词与单词列表中的大小写相同。
|
||||
- 例如:wordlist = ["yellow"], query = "YellOw": correct = "yellow"
|
||||
- 例如:wordlist = ["Yellow"], query = "yellow": correct = "Yellow"
|
||||
- 例如:wordlist = ["yellow"], query = "yellow": correct = "yellow"
|
||||
- 元音错误:如果在将查询单词中的元音(‘a’、‘e’、‘i’、‘o’、‘u’)分别替换为任何元音后,能与单词列表中的单词匹配(不区分大小写),则返回的正确单词与单词列表中的匹配项大小写相同。
|
||||
- 例如:wordlist = ["YellOw"], query = "yollow": correct = "YellOw"
|
||||
- 例如:wordlist = ["YellOw"], query = "yeellow": correct = "" (无匹配项)
|
||||
- 例如:wordlist = ["YellOw"], query = "yllw": correct = "" (无匹配项)
|
||||
|
||||
此外,拼写检查器还按照以下优先级规则操作:
|
||||
|
||||
- 当查询完全匹配单词列表中的某个单词(区分大小写)时,应返回相同的单词。
|
||||
- 当查询匹配到大小写问题的单词时,您应该返回单词列表中的第一个这样的匹配项。
|
||||
- 当查询匹配到元音错误的单词时,您应该返回单词列表中的第一个这样的匹配项。
|
||||
- 如果该查询在单词列表中没有匹配项,则应返回空字符串。
|
||||
|
||||
给出一些查询 queries,返回一个单词列表 answer,其中 answer[i] 是由查询 query = queries[i] 得到的正确单词。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 读完题,很明显需要用 `map` 来解题。依题意分为 3 种情况,查询字符串完全匹配;查询字符串只是大小写不同;查询字符串有元音错误。第一种情况用 `map` `key` 直接匹配即可。第二种情况,利用 `map` 将单词从小写形式转换成原单词正确的大小写形式。第三种情况,利用 `map` 将单词从忽略元音的小写形式换成原单词正确形式。最后注意一下题目最后给的 4 个优先级规则即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
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)
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user