Skip to content

Add solution and test-cases for problem 966 #1247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions leetcode/901-1000/0966.Vowel-Spellchecker/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
# [966.Vowel Spellchecker][title]

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

## Description

Given a `wordlist`, we want to implement a spellchecker that converts a query word into a correct word.

For a given `quer` 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: a = "11", b = "1"
Output: "100"
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:**

## 题解

### 思路1
> ...
Vowel Spellchecker
```go
```

Input: wordlist = ["yellow"], queries = ["YellOw"]
Output: ["yellow"]
```

## 结语

Expand Down
64 changes: 62 additions & 2 deletions leetcode/901-1000/0966.Vowel-Spellchecker/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,65 @@
package Solution

func Solution(x bool) bool {
return x
import (
"fmt"
"strings"
)

func isVowel(b byte) bool {
return b == 'a' || b == 'e' || b == 'i' || b == 'o' || b == 'u' ||
b == 'A' || b == 'E' || b == 'I' || b == 'O' || b == 'U'
}

// babee = b1b2
func toVowel(word string) string {
buf := strings.Builder{}
c := 0
for _, b := range []byte(word) {
if !isVowel(b) {
buf.WriteByte(b)
continue
}
buf.WriteString(fmt.Sprintf("%d", c))
c = 0
}
if c > 0 {
buf.WriteString(fmt.Sprintf("%d", c))
}

return buf.String()
}

func Solution(wordlist []string, queries []string) []string {
m := make(map[string]struct{})
lower := make(map[string]string)
matched := make(map[string]string)
for _, word := range wordlist {
m[word] = struct{}{}
l := strings.ToLower(word)
if _, ok := lower[l]; !ok {
lower[l] = word
}
match := toVowel(l)
if _, ok := matched[match]; !ok {
matched[match] = word
}
}
ans := make([]string, len(queries))
for i, q := range queries {
if _, ok := m[q]; ok {
ans[i] = q
continue
}
lq := strings.ToLower(q)

if v, ok := lower[lq]; ok {
ans[i] = v
continue
}
if v, ok := matched[toVowel(lq)]; ok {
ans[i] = v
continue
}
}
return ans
}
21 changes: 10 additions & 11 deletions leetcode/901-1000/0966.Vowel-Spellchecker/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,30 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
wordlist, queries []string
expect []string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"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"}},
{"TestCase2", []string{"yellow"}, []string{"YellOw"}, []string{"yellow"}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.wordlist, c.queries)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.wordlist, c.queries)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading