Skip to content

Commit 2ada11b

Browse files
authored
Merge pull request #1247 from 0xff-dev/966
Add solution and test-cases for problem 966
2 parents 76cfb26 + 433ecd6 commit 2ada11b

File tree

3 files changed

+103
-27
lines changed

3 files changed

+103
-27
lines changed

leetcode/901-1000/0966.Vowel-Spellchecker/README.md

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
# [966.Vowel Spellchecker][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
74

5+
Given a `wordlist`, we want to implement a spellchecker that converts a query word into a correct word.
6+
7+
For a given `quer` word, the spell checker handles two categories of spelling mistakes:
8+
9+
- 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.
10+
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+
15+
- 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.
16+
17+
- Example: `wordlist = ["YellOw"]`, `query = "yollow"`: `correct = "YellOw"`
18+
- Example: `wordlist = ["YellOw"]`, `query = "yeellow"`: `correct = ""` (no match)
19+
- Example: `wordlist = ["YellOw"]`, `query = "yllw"`: `correct = ""` (no match)
20+
21+
In addition, the spell checker operates under the following precedence rules:
22+
23+
- When the query exactly matches a word in the wordlist (**case-sensitive**), you should return the same word back.
24+
- When the query matches a word up to capitlization, you should return the first such match in the wordlist.
25+
- When the query matches a word up to vowel errors, you should return the first such match in the wordlist.
26+
- If the query has no matches in the wordlist, you should return the empty string.
27+
28+
Given some `queries`, return a list of words `answer`, where `answer[i]` is the correct word for `query = queries[i]`.
29+
830
**Example 1:**
931

1032
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
33+
Input: wordlist = ["KiTe","kite","hare","Hare"], queries = ["kite","Kite","KiTe","Hare","HARE","Hear","hear","keti","keet","keto"]
34+
Output: ["kite","KiTe","KiTe","Hare","hare","","","KiTe","","KiTe"]
1335
```
1436

15-
## 题意
16-
> ...
37+
**Example 2:**
1738

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Vowel Spellchecker
23-
```go
2439
```
25-
40+
Input: wordlist = ["yellow"], queries = ["YellOw"]
41+
Output: ["yellow"]
42+
```
2643

2744
## 结语
2845

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,65 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
func isVowel(b byte) bool {
9+
return b == 'a' || b == 'e' || b == 'i' || b == 'o' || b == 'u' ||
10+
b == 'A' || b == 'E' || b == 'I' || b == 'O' || b == 'U'
11+
}
12+
13+
// babee = b1b2
14+
func toVowel(word string) string {
15+
buf := strings.Builder{}
16+
c := 0
17+
for _, b := range []byte(word) {
18+
if !isVowel(b) {
19+
buf.WriteByte(b)
20+
continue
21+
}
22+
buf.WriteString(fmt.Sprintf("%d", c))
23+
c = 0
24+
}
25+
if c > 0 {
26+
buf.WriteString(fmt.Sprintf("%d", c))
27+
}
28+
29+
return buf.String()
30+
}
31+
32+
func Solution(wordlist []string, queries []string) []string {
33+
m := make(map[string]struct{})
34+
lower := make(map[string]string)
35+
matched := make(map[string]string)
36+
for _, word := range wordlist {
37+
m[word] = struct{}{}
38+
l := strings.ToLower(word)
39+
if _, ok := lower[l]; !ok {
40+
lower[l] = word
41+
}
42+
match := toVowel(l)
43+
if _, ok := matched[match]; !ok {
44+
matched[match] = word
45+
}
46+
}
47+
ans := make([]string, len(queries))
48+
for i, q := range queries {
49+
if _, ok := m[q]; ok {
50+
ans[i] = q
51+
continue
52+
}
53+
lq := strings.ToLower(q)
54+
55+
if v, ok := lower[lq]; ok {
56+
ans[i] = v
57+
continue
58+
}
59+
if v, ok := matched[toVowel(lq)]; ok {
60+
ans[i] = v
61+
continue
62+
}
63+
}
64+
return ans
565
}

leetcode/901-1000/0966.Vowel-Spellchecker/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
wordlist, queries []string
14+
expect []string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []string{"KiTe", "kite", "hare", "Hare"}, []string{"kite", "Kite", "KiTe", "Hare", "HARE", "Hear", "hear", "keti", "keet", "keto"}, []string{"kite", "KiTe", "KiTe", "Hare", "hare", "", "", "KiTe", "", "KiTe"}},
17+
{"TestCase2", []string{"yellow"}, []string{"YellOw"}, []string{"yellow"}},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.wordlist, c.queries)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.wordlist, c.queries)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)