From 2982175a6f6ee700aaf8caa790107ae2e3f2ed2a Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 8 Jan 2025 09:30:36 +0800 Subject: [PATCH] Add solution and test-cases for problem 3042 --- .../README.md | 46 +++++++++++++------ .../Solution.go | 14 +++++- .../Solution_test.go | 14 +++--- 3 files changed, 52 insertions(+), 22 deletions(-) diff --git a/leetcode/3001-3100/3042.Count-Prefix-and-Suffix-Pairs-I/README.md b/leetcode/3001-3100/3042.Count-Prefix-and-Suffix-Pairs-I/README.md index 6fdb7b9fb..598410c2b 100755 --- a/leetcode/3001-3100/3042.Count-Prefix-and-Suffix-Pairs-I/README.md +++ b/leetcode/3001-3100/3042.Count-Prefix-and-Suffix-Pairs-I/README.md @@ -1,28 +1,48 @@ # [3042.Count Prefix and Suffix Pairs I][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 a **0-indexed** string array `words`. + +Let's define a **boolean** function `isPrefixAndSuffix` that takes two strings, `str1` and `str2`: + +- `isPrefixAndSuffix(str1, str2)` returns `true` if `str1` is **both** a `prefix` and a `suffix` of `str2`, and `false` otherwise. + +For example, `isPrefixAndSuffix("aba", "ababa")` is `true` because `"aba"` is a prefix of `"ababa"` and also a suffix, but `isPrefixAndSuffix("abc", "abcd")` is `false`. + +Return an integer denoting the **number** of index pairs `(i, j)` such that `i < j`, and `isPrefixAndSuffix(words[i], words[j])` is `true`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: words = ["a","aba","ababa","aa"] +Output: 4 +Explanation: In this example, the counted index pairs are: +i = 0 and j = 1 because isPrefixAndSuffix("a", "aba") is true. +i = 0 and j = 2 because isPrefixAndSuffix("a", "ababa") is true. +i = 0 and j = 3 because isPrefixAndSuffix("a", "aa") is true. +i = 1 and j = 2 because isPrefixAndSuffix("aba", "ababa") is true. +Therefore, the answer is 4. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Count Prefix and Suffix Pairs I -```go ``` +Input: words = ["pa","papa","ma","mama"] +Output: 2 +Explanation: In this example, the counted index pairs are: +i = 0 and j = 1 because isPrefixAndSuffix("pa", "papa") is true. +i = 2 and j = 3 because isPrefixAndSuffix("ma", "mama") is true. +Therefore, the answer is 2. +``` + +**Example 3:** +``` +Input: words = ["abab","ab"] +Output: 0 +Explanation: In this example, the only valid index pair is i = 0 and j = 1, and isPrefixAndSuffix("abab", "ab") is false. +Therefore, the answer is 0. +``` ## 结语 diff --git a/leetcode/3001-3100/3042.Count-Prefix-and-Suffix-Pairs-I/Solution.go b/leetcode/3001-3100/3042.Count-Prefix-and-Suffix-Pairs-I/Solution.go index d115ccf5e..9a660dfc6 100644 --- a/leetcode/3001-3100/3042.Count-Prefix-and-Suffix-Pairs-I/Solution.go +++ b/leetcode/3001-3100/3042.Count-Prefix-and-Suffix-Pairs-I/Solution.go @@ -1,5 +1,15 @@ package Solution -func Solution(x bool) bool { - return x +import "strings" + +func Solution(words []string) int { + ans := 0 + for i := 0; i < len(words); i++ { + for j := i + 1; j < len(words); j++ { + if strings.HasPrefix(words[j], words[i]) && strings.HasSuffix(words[j], words[i]) { + ans++ + } + } + } + return ans } diff --git a/leetcode/3001-3100/3042.Count-Prefix-and-Suffix-Pairs-I/Solution_test.go b/leetcode/3001-3100/3042.Count-Prefix-and-Suffix-Pairs-I/Solution_test.go index 14ff50eb4..df3228496 100644 --- a/leetcode/3001-3100/3042.Count-Prefix-and-Suffix-Pairs-I/Solution_test.go +++ b/leetcode/3001-3100/3042.Count-Prefix-and-Suffix-Pairs-I/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []string + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []string{"a", "aba", "ababa", "aa"}, 4}, + {"TestCase2", []string{"pa", "papa", "ma", "mama"}, 2}, + {"TestCase3", []string{"abab", "ab"}, 0}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }