Skip to content
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

Add solution and test-cases for problem 809 #978

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions leetcode/801-900/0809.Expressive-Words/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
# [809.Expressive Words][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
Sometimes people repeat letters to represent extra feeling. For example:

- `"hello" -> "heeellooo"`
- `"hi" -> "hiiii"`

In these strings like `"heeellooo"`, we have groups of adjacent letters that are all the same: `"h"`, `"eee"`, `"ll"`, `"ooo"`.

You are given a string `s` and an array of query strings `words`. A query word is **strectchy** if it can be made to be equal to `s` by any number of applications of the following extension operation: choose a group consisting of characters `c`, and add some number of characters `c` to the group so that the size of the group is **three or more**.

- For example, starting with `"hello"`, we could do an extension on the group `"o"` to get `"hellooo"`, but we cannot get `"helloo"` since the group `"oo"` has a size less than three. Also, we could do another extension like `"ll" -> "lllll"` to get `"helllllooo"`. If `s = "helllllooo"`, then the query word `"hello"` would be **strectchy** because of these two extension operations: `query = "hello" -> "hellooo" -> "helllllooo" = s`.

Return the number of query strings that are **strectchy**.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "heeellooo", words = ["hello", "hi", "helo"]
Output: 1
Explanation:
We can extend "e" and "o" in the word "hello" to get "heeellooo".
We can't extend "helo" to get "heeellooo" because the group "ll" is not size 3 or more.
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Expressive Words
```go
```

Input: s = "zzzzzyyyyy", words = ["zzyy","zy","zyy"]
Output: 3
```

## 结语

Expand Down
52 changes: 50 additions & 2 deletions leetcode/801-900/0809.Expressive-Words/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,53 @@
package Solution

func Solution(x bool) bool {
return x
type char809 struct {
c byte
cc int
}

func toChars(s string) []char809 {
pre := s[0]
c := 1
chars := make([]char809, 0)
for i := 1; i < len(s); i++ {
b := s[i]
if b == pre {
c++
continue
}
chars = append(chars, char809{c: byte(pre), cc: c})
pre = b
c = 1
}
chars = append(chars, char809{c: byte(pre), cc: c})
return chars
}
func Solution(s string, words []string) int {
ans := 0
target := toChars(s)
for _, word := range words {
tmp := toChars(word)
if len(tmp) != len(target) {
continue
}
ok := true
for i := range len(target) {
if target[i].c != tmp[i].c {
ok = false
break
}
if target[i].cc < 3 && tmp[i].cc != target[i].cc {
ok = false
break
}
if tmp[i].cc > target[i].cc {
ok = false
break
}
}
if ok {
ans++
}
}
return ans
}
20 changes: 10 additions & 10 deletions leetcode/801-900/0809.Expressive-Words/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
s string
words []string
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "heeellooo", []string{"hello", "hi", "helo"}, 1},
{"TestCase2", "zzzzzyyyyy", []string{"zzyy", "zy", "zyy"}, 3},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.s, c.words)
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.s, c.words)
}
})
}
}

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

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