Skip to content

Commit

Permalink
Merge pull request #989 from 0xff-dev/1078
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1078
  • Loading branch information
6boris authored Oct 30, 2024
2 parents 94117b6 + 150cb11 commit 6821fb3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
23 changes: 9 additions & 14 deletions leetcode/1001-1100/1078.Occurrences-After-Bigram/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
# [1078.Occurrences After Bigram][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 two strings `first` and `second`, consider occurrences in some text of the form `"first second third"`, where `second` comes immediately after `first`, and `third` comes immediately after `second`.

Return an array of all the words `third` for each occurrence of `"first second third"`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: text = "alice is a good girl she is a good student", first = "a", second = "good"
Output: ["girl","student"]
```

## 题意
> ...
## 题解
**Example 2:**

### 思路1
> ...
Occurrences After Bigram
```go
```

Input: text = "we will we will rock you", first = "we", second = "will"
Output: ["we","rock"]
```

## 结语

Expand Down
23 changes: 21 additions & 2 deletions leetcode/1001-1100/1078.Occurrences-After-Bigram/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
package Solution

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

func Solution(text string, first string, second string) []string {
testlist := strings.Split(text, " ")
fm := make(map[string]struct{})
sm := make(map[string]struct{})
for _, w := range strings.Split(first, " ") {
fm[w] = struct{}{}
}
for _, w := range strings.Split(second, " ") {
sm[w] = struct{}{}
}
ans := make([]string, 0)
for i := 0; i < len(testlist)-2; i++ {
_, ok1 := fm[testlist[i]]
_, ok2 := sm[testlist[i+1]]
if ok1 && ok2 {
ans = append(ans, testlist[i+2])
}
}
return ans
}
21 changes: 10 additions & 11 deletions leetcode/1001-1100/1078.Occurrences-After-Bigram/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
a, b, c string
expect []string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "alice is a good girl she is a good student", "a", "good", []string{"girl", "student"}},
{"TestCase2", "we will we will rock you", "we", "will", []string{"we", "rock"}},
}

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

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

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

0 comments on commit 6821fb3

Please sign in to comment.