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 916 #962

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
29 changes: 15 additions & 14 deletions leetcode/901-1000/0916.Word-Subsets/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
# [916.Word Subsets][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
You are given two string arrays `words1` and `words2`.

A string `b` is a **subset** of string `a` if every letter in `b` occurs in `a` including multiplicity.

- For example, `"wrr"` is a subset of `"warrior"` but is not a subset of `"world"`.

A string `a` from `words1` is **universal** if for every string `b` in `words2`, `b` is a subset of `a`.

Return an array of all the **universal** strings in `words1`. You may return the answer in **any order**.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["e","o"]
Output: ["facebook","google","leetcode"]
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Word Subsets
```go
```

Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["l","e"]
Output: ["apple","google","leetcode"]
```

## 结语

Expand Down
33 changes: 31 additions & 2 deletions leetcode/901-1000/0916.Word-Subsets/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(words1 []string, words2 []string) []string {
ac := [26]int{}
for _, w := range words2 {
tmp := [26]int{}
for _, b := range w {
tmp[b-'a']++
}
for i := range 26 {
if tmp[i] > ac[i] {
ac[i] = tmp[i]
}
}
}
ans := make([]string, 0)
for _, a := range words1 {
tmp := [26]int{}
for _, b := range a {
tmp[b-'a']++
}
ok := true
for i := range 26 {
if ac[i] > tmp[i] {
ok = false
break
}
}
if ok {
ans = append(ans, a)
}
}
return ans
}
19 changes: 9 additions & 10 deletions leetcode/901-1000/0916.Word-Subsets/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,29 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
w1, w2 []string
expect []string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []string{"amazon", "apple", "facebook", "google", "leetcode"}, []string{"e", "o"}, []string{"facebook", "google", "leetcode"}},
{"TestCase2", []string{"amazon", "apple", "facebook", "google", "leetcode"}, []string{"l", "e"}, []string{"apple", "google", "leetcode"}},
}

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

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

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