From 3236115a83a539049977b06f6f4d3c9ac8fa3695 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sun, 18 Aug 2024 16:17:25 +0800 Subject: [PATCH] Add solution and test-cases for problem 916 --- leetcode/901-1000/0916.Word-Subsets/README.md | 29 ++++++++-------- .../901-1000/0916.Word-Subsets/Solution.go | 33 +++++++++++++++++-- .../0916.Word-Subsets/Solution_test.go | 19 +++++------ 3 files changed, 55 insertions(+), 26 deletions(-) diff --git a/leetcode/901-1000/0916.Word-Subsets/README.md b/leetcode/901-1000/0916.Word-Subsets/README.md index 7b793ed23..6b529d2ab 100644 --- a/leetcode/901-1000/0916.Word-Subsets/README.md +++ b/leetcode/901-1000/0916.Word-Subsets/README.md @@ -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"] +``` ## 结语 diff --git a/leetcode/901-1000/0916.Word-Subsets/Solution.go b/leetcode/901-1000/0916.Word-Subsets/Solution.go index d115ccf5e..7cc904bd4 100644 --- a/leetcode/901-1000/0916.Word-Subsets/Solution.go +++ b/leetcode/901-1000/0916.Word-Subsets/Solution.go @@ -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 } diff --git a/leetcode/901-1000/0916.Word-Subsets/Solution_test.go b/leetcode/901-1000/0916.Word-Subsets/Solution_test.go index 14ff50eb4..bbfc0cd08 100644 --- a/leetcode/901-1000/0916.Word-Subsets/Solution_test.go +++ b/leetcode/901-1000/0916.Word-Subsets/Solution_test.go @@ -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() { }