Skip to content

Commit fec98db

Browse files
committed
Add solution 966
1 parent 613fa9a commit fec98db

24 files changed

+669
-311
lines changed

README.md

Lines changed: 236 additions & 236 deletions
Large diffs are not rendered by default.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package leetcode
2+
3+
import "strings"
4+
5+
func spellchecker(wordlist []string, queries []string) []string {
6+
wordsPerfect, wordsCap, wordsVowel := map[string]bool{}, map[string]string{}, map[string]string{}
7+
for _, word := range wordlist {
8+
wordsPerfect[word] = true
9+
wordLow := strings.ToLower(word)
10+
if _, ok := wordsCap[wordLow]; !ok {
11+
wordsCap[wordLow] = word
12+
}
13+
wordLowVowel := devowel(wordLow)
14+
if _, ok := wordsVowel[wordLowVowel]; !ok {
15+
wordsVowel[wordLowVowel] = word
16+
}
17+
}
18+
res, index := make([]string, len(queries)), 0
19+
for _, query := range queries {
20+
if _, ok := wordsPerfect[query]; ok {
21+
res[index] = query
22+
index++
23+
continue
24+
}
25+
queryL := strings.ToLower(query)
26+
if v, ok := wordsCap[queryL]; ok {
27+
res[index] = v
28+
index++
29+
continue
30+
}
31+
32+
queryLV := devowel(queryL)
33+
if v, ok := wordsVowel[queryLV]; ok {
34+
res[index] = v
35+
index++
36+
continue
37+
}
38+
res[index] = ""
39+
index++
40+
}
41+
return res
42+
43+
}
44+
45+
func devowel(word string) string {
46+
runes := []rune(word)
47+
for k, c := range runes {
48+
if c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' {
49+
runes[k] = '*'
50+
}
51+
}
52+
return string(runes)
53+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question966 struct {
9+
para966
10+
ans966
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para966 struct {
16+
wordlist []string
17+
queries []string
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans966 struct {
23+
one []string
24+
}
25+
26+
func Test_Problem966(t *testing.T) {
27+
28+
qs := []question966{
29+
{
30+
para966{[]string{"KiTe", "kite", "hare", "Hare"}, []string{"kite", "Kite", "KiTe", "Hare", "HARE", "Hear", "hear", "keti", "keet", "keto"}},
31+
ans966{[]string{"kite", "KiTe", "KiTe", "Hare", "hare", "", "", "KiTe", "", "KiTe"}},
32+
},
33+
}
34+
35+
fmt.Printf("------------------------Leetcode Problem 966------------------------\n")
36+
37+
for _, q := range qs {
38+
_, p := q.ans966, q.para966
39+
fmt.Printf("【input】:%v 【output】:%#v\n", p, spellchecker(p.wordlist, p.queries))
40+
}
41+
fmt.Printf("\n\n\n")
42+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# [966. Vowel Spellchecker](https://leetcode.com/problems/vowel-spellchecker/)
2+
3+
4+
## 题目
5+
6+
Given a `wordlist`, we want to implement a spellchecker that converts a query word into a correct word.
7+
8+
For a given `query` word, the spell checker handles two categories of spelling mistakes:
9+
10+
- 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.
11+
- Example: `wordlist = ["yellow"]``query = "YellOw"``correct = "yellow"`
12+
- Example: `wordlist = ["Yellow"]``query = "yellow"``correct = "Yellow"`
13+
- Example: `wordlist = ["yellow"]``query = "yellow"``correct = "yellow"`
14+
- 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.
15+
- Example: `wordlist = ["YellOw"]``query = "yollow"``correct = "YellOw"`
16+
- Example: `wordlist = ["YellOw"]``query = "yeellow"``correct = ""` (no match)
17+
- Example: `wordlist = ["YellOw"]``query = "yllw"``correct = ""` (no match)
18+
19+
In addition, the spell checker operates under the following precedence rules:
20+
21+
- When the query exactly matches a word in the wordlist (**case-sensitive**), you should return the same word back.
22+
- When the query matches a word up to capitlization, you should return the first such match in the wordlist.
23+
- When the query matches a word up to vowel errors, you should return the first such match in the wordlist.
24+
- If the query has no matches in the wordlist, you should return the empty string.
25+
26+
Given some `queries`, return a list of words `answer`, where `answer[i]` is the correct word for `query = queries[i]`.
27+
28+
**Example 1:**
29+
30+
```
31+
Input:wordlist = ["KiTe","kite","hare","Hare"], queries = ["kite","Kite","KiTe","Hare","HARE","Hear","hear","keti","keet","keto"]
32+
Output:["kite","KiTe","KiTe","Hare","hare","","","KiTe","","KiTe"]
33+
```
34+
35+
**Note:**
36+
37+
- `1 <= wordlist.length <= 5000`
38+
- `1 <= queries.length <= 5000`
39+
- `1 <= wordlist[i].length <= 7`
40+
- `1 <= queries[i].length <= 7`
41+
- All strings in `wordlist` and `queries` consist only of **english** letters.
42+
43+
## 题目大意
44+
45+
在给定单词列表 wordlist 的情况下,我们希望实现一个拼写检查器,将查询单词转换为正确的单词。
46+
47+
对于给定的查询单词 query,拼写检查器将会处理两类拼写错误:
48+
49+
- 大小写:如果查询匹配单词列表中的某个单词(不区分大小写),则返回的正确单词与单词列表中的大小写相同。
50+
- 例如:wordlist = ["yellow"], query = "YellOw": correct = "yellow"
51+
- 例如:wordlist = ["Yellow"], query = "yellow": correct = "Yellow"
52+
- 例如:wordlist = ["yellow"], query = "yellow": correct = "yellow"
53+
- 元音错误:如果在将查询单词中的元音(‘a’、‘e’、‘i’、‘o’、‘u’)分别替换为任何元音后,能与单词列表中的单词匹配(不区分大小写),则返回的正确单词与单词列表中的匹配项大小写相同。
54+
- 例如:wordlist = ["YellOw"], query = "yollow": correct = "YellOw"
55+
- 例如:wordlist = ["YellOw"], query = "yeellow": correct = "" (无匹配项)
56+
- 例如:wordlist = ["YellOw"], query = "yllw": correct = "" (无匹配项)
57+
58+
此外,拼写检查器还按照以下优先级规则操作:
59+
60+
- 当查询完全匹配单词列表中的某个单词(区分大小写)时,应返回相同的单词。
61+
- 当查询匹配到大小写问题的单词时,您应该返回单词列表中的第一个这样的匹配项。
62+
- 当查询匹配到元音错误的单词时,您应该返回单词列表中的第一个这样的匹配项。
63+
- 如果该查询在单词列表中没有匹配项,则应返回空字符串。
64+
65+
给出一些查询 queries,返回一个单词列表 answer,其中 answer[i] 是由查询 query = queries[i] 得到的正确单词。
66+
67+
## 解题思路
68+
69+
- 读完题,很明显需要用 `map` 来解题。依题意分为 3 种情况,查询字符串完全匹配;查询字符串只是大小写不同;查询字符串有元音错误。第一种情况用 `map` `key` 直接匹配即可。第二种情况,利用 `map` 将单词从小写形式转换成原单词正确的大小写形式。第三种情况,利用 `map` 将单词从忽略元音的小写形式换成原单词正确形式。最后注意一下题目最后给的 4 个优先级规则即可。
70+
71+
## 代码
72+
73+
```go
74+
package leetcode
75+
76+
import "strings"
77+
78+
func spellchecker(wordlist []string, queries []string) []string {
79+
wordsPerfect, wordsCap, wordsVowel := map[string]bool{}, map[string]string{}, map[string]string{}
80+
for _, word := range wordlist {
81+
wordsPerfect[word] = true
82+
wordLow := strings.ToLower(word)
83+
if _, ok := wordsCap[wordLow]; !ok {
84+
wordsCap[wordLow] = word
85+
}
86+
wordLowVowel := devowel(wordLow)
87+
if _, ok := wordsVowel[wordLowVowel]; !ok {
88+
wordsVowel[wordLowVowel] = word
89+
}
90+
}
91+
res, index := make([]string, len(queries)), 0
92+
for _, query := range queries {
93+
if _, ok := wordsPerfect[query]; ok {
94+
res[index] = query
95+
index++
96+
continue
97+
}
98+
queryL := strings.ToLower(query)
99+
if v, ok := wordsCap[queryL]; ok {
100+
res[index] = v
101+
index++
102+
continue
103+
}
104+
105+
queryLV := devowel(queryL)
106+
if v, ok := wordsVowel[queryLV]; ok {
107+
res[index] = v
108+
index++
109+
continue
110+
}
111+
res[index] = ""
112+
index++
113+
}
114+
return res
115+
116+
}
117+
118+
func devowel(word string) string {
119+
runes := []rune(word)
120+
for k, c := range runes {
121+
if c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' {
122+
runes[k] = '*'
123+
}
124+
}
125+
return string(runes)
126+
}
127+
```

website/content/ChapterFour/0900~0999/0961.N-Repeated-Element-in-Size-2N-Array.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,5 @@ func repeatedNTimes(A []int) int {
6363
----------------------------------------------
6464
<div style="display: flex;justify-content: space-between;align-items: center;">
6565
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0900~0999/0959.Regions-Cut-By-Slashes/">⬅️上一页</a></p>
66-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0900~0999/0968.Binary-Tree-Cameras/">下一页➡️</a></p>
66+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0900~0999/0966.Vowel-Spellchecker/">下一页➡️</a></p>
6767
</div>
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# [966. Vowel Spellchecker](https://leetcode.com/problems/vowel-spellchecker/)
2+
3+
4+
## 题目
5+
6+
Given a `wordlist`, we want to implement a spellchecker that converts a query word into a correct word.
7+
8+
For a given `query` word, the spell checker handles two categories of spelling mistakes:
9+
10+
- 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.
11+
- Example: `wordlist = ["yellow"]``query = "YellOw"``correct = "yellow"`
12+
- Example: `wordlist = ["Yellow"]``query = "yellow"``correct = "Yellow"`
13+
- Example: `wordlist = ["yellow"]``query = "yellow"``correct = "yellow"`
14+
- 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.
15+
- Example: `wordlist = ["YellOw"]``query = "yollow"``correct = "YellOw"`
16+
- Example: `wordlist = ["YellOw"]``query = "yeellow"``correct = ""` (no match)
17+
- Example: `wordlist = ["YellOw"]``query = "yllw"``correct = ""` (no match)
18+
19+
In addition, the spell checker operates under the following precedence rules:
20+
21+
- When the query exactly matches a word in the wordlist (**case-sensitive**), you should return the same word back.
22+
- When the query matches a word up to capitlization, you should return the first such match in the wordlist.
23+
- When the query matches a word up to vowel errors, you should return the first such match in the wordlist.
24+
- If the query has no matches in the wordlist, you should return the empty string.
25+
26+
Given some `queries`, return a list of words `answer`, where `answer[i]` is the correct word for `query = queries[i]`.
27+
28+
**Example 1:**
29+
30+
```
31+
Input:wordlist = ["KiTe","kite","hare","Hare"], queries = ["kite","Kite","KiTe","Hare","HARE","Hear","hear","keti","keet","keto"]
32+
Output:["kite","KiTe","KiTe","Hare","hare","","","KiTe","","KiTe"]
33+
```
34+
35+
**Note:**
36+
37+
- `1 <= wordlist.length <= 5000`
38+
- `1 <= queries.length <= 5000`
39+
- `1 <= wordlist[i].length <= 7`
40+
- `1 <= queries[i].length <= 7`
41+
- All strings in `wordlist` and `queries` consist only of **english** letters.
42+
43+
## 题目大意
44+
45+
在给定单词列表 wordlist 的情况下,我们希望实现一个拼写检查器,将查询单词转换为正确的单词。
46+
47+
对于给定的查询单词 query,拼写检查器将会处理两类拼写错误:
48+
49+
- 大小写:如果查询匹配单词列表中的某个单词(不区分大小写),则返回的正确单词与单词列表中的大小写相同。
50+
- 例如:wordlist = ["yellow"], query = "YellOw": correct = "yellow"
51+
- 例如:wordlist = ["Yellow"], query = "yellow": correct = "Yellow"
52+
- 例如:wordlist = ["yellow"], query = "yellow": correct = "yellow"
53+
- 元音错误:如果在将查询单词中的元音(‘a’、‘e’、‘i’、‘o’、‘u’)分别替换为任何元音后,能与单词列表中的单词匹配(不区分大小写),则返回的正确单词与单词列表中的匹配项大小写相同。
54+
- 例如:wordlist = ["YellOw"], query = "yollow": correct = "YellOw"
55+
- 例如:wordlist = ["YellOw"], query = "yeellow": correct = "" (无匹配项)
56+
- 例如:wordlist = ["YellOw"], query = "yllw": correct = "" (无匹配项)
57+
58+
此外,拼写检查器还按照以下优先级规则操作:
59+
60+
- 当查询完全匹配单词列表中的某个单词(区分大小写)时,应返回相同的单词。
61+
- 当查询匹配到大小写问题的单词时,您应该返回单词列表中的第一个这样的匹配项。
62+
- 当查询匹配到元音错误的单词时,您应该返回单词列表中的第一个这样的匹配项。
63+
- 如果该查询在单词列表中没有匹配项,则应返回空字符串。
64+
65+
给出一些查询 queries,返回一个单词列表 answer,其中 answer[i] 是由查询 query = queries[i] 得到的正确单词。
66+
67+
## 解题思路
68+
69+
- 读完题,很明显需要用 `map` 来解题。依题意分为 3 种情况,查询字符串完全匹配;查询字符串只是大小写不同;查询字符串有元音错误。第一种情况用 `map` `key` 直接匹配即可。第二种情况,利用 `map` 将单词从小写形式转换成原单词正确的大小写形式。第三种情况,利用 `map` 将单词从忽略元音的小写形式换成原单词正确形式。最后注意一下题目最后给的 4 个优先级规则即可。
70+
71+
## 代码
72+
73+
```go
74+
package leetcode
75+
76+
import "strings"
77+
78+
func spellchecker(wordlist []string, queries []string) []string {
79+
wordsPerfect, wordsCap, wordsVowel := map[string]bool{}, map[string]string{}, map[string]string{}
80+
for _, word := range wordlist {
81+
wordsPerfect[word] = true
82+
wordLow := strings.ToLower(word)
83+
if _, ok := wordsCap[wordLow]; !ok {
84+
wordsCap[wordLow] = word
85+
}
86+
wordLowVowel := devowel(wordLow)
87+
if _, ok := wordsVowel[wordLowVowel]; !ok {
88+
wordsVowel[wordLowVowel] = word
89+
}
90+
}
91+
res, index := make([]string, len(queries)), 0
92+
for _, query := range queries {
93+
if _, ok := wordsPerfect[query]; ok {
94+
res[index] = query
95+
index++
96+
continue
97+
}
98+
queryL := strings.ToLower(query)
99+
if v, ok := wordsCap[queryL]; ok {
100+
res[index] = v
101+
index++
102+
continue
103+
}
104+
105+
queryLV := devowel(queryL)
106+
if v, ok := wordsVowel[queryLV]; ok {
107+
res[index] = v
108+
index++
109+
continue
110+
}
111+
res[index] = ""
112+
index++
113+
}
114+
return res
115+
116+
}
117+
118+
func devowel(word string) string {
119+
runes := []rune(word)
120+
for k, c := range runes {
121+
if c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' {
122+
runes[k] = '*'
123+
}
124+
}
125+
return string(runes)
126+
}
127+
```
128+
129+
130+
----------------------------------------------
131+
<div style="display: flex;justify-content: space-between;align-items: center;">
132+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0900~0999/0961.N-Repeated-Element-in-Size-2N-Array/">⬅️上一页</a></p>
133+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0900~0999/0968.Binary-Tree-Cameras/">下一页➡️</a></p>
134+
</div>

website/content/ChapterFour/0900~0999/0968.Binary-Tree-Cameras.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,6 @@ func minCameraCoverDFS(root *TreeNode, res *int) status {
105105

106106
----------------------------------------------
107107
<div style="display: flex;justify-content: space-between;align-items: center;">
108-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0900~0999/0961.N-Repeated-Element-in-Size-2N-Array/">⬅️上一页</a></p>
108+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0900~0999/0966.Vowel-Spellchecker/">⬅️上一页</a></p>
109109
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0900~0999/0969.Pancake-Sorting/">下一页➡️</a></p>
110110
</div>

0 commit comments

Comments
 (0)