Skip to content

Commit

Permalink
Add solution and test-cases for problem 3042
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Jan 8, 2025
1 parent a337ff8 commit 2982175
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
46 changes: 33 additions & 13 deletions leetcode/3001-3100/3042.Count-Prefix-and-Suffix-Pairs-I/README.md
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

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

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

0 comments on commit 2982175

Please sign in to comment.