Skip to content

Commit 2982175

Browse files
committed
Add solution and test-cases for problem 3042
1 parent a337ff8 commit 2982175

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

leetcode/3001-3100/3042.Count-Prefix-and-Suffix-Pairs-I/README.md

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,48 @@
11
# [3042.Count Prefix and Suffix Pairs I][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a **0-indexed** string array `words`.
5+
6+
Let's define a **boolean** function `isPrefixAndSuffix` that takes two strings, `str1` and `str2`:
7+
8+
- `isPrefixAndSuffix(str1, str2)` returns `true` if `str1` is **both** a `prefix` and a `suffix` of `str2`, and `false` otherwise.
9+
10+
For example, `isPrefixAndSuffix("aba", "ababa")` is `true` because `"aba"` is a prefix of `"ababa"` and also a suffix, but `isPrefixAndSuffix("abc", "abcd")` is `false`.
11+
12+
Return an integer denoting the **number** of index pairs `(i, j)` such that `i < j`, and `isPrefixAndSuffix(words[i], words[j])` is `true`.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: words = ["a","aba","ababa","aa"]
18+
Output: 4
19+
Explanation: In this example, the counted index pairs are:
20+
i = 0 and j = 1 because isPrefixAndSuffix("a", "aba") is true.
21+
i = 0 and j = 2 because isPrefixAndSuffix("a", "ababa") is true.
22+
i = 0 and j = 3 because isPrefixAndSuffix("a", "aa") is true.
23+
i = 1 and j = 2 because isPrefixAndSuffix("aba", "ababa") is true.
24+
Therefore, the answer is 4.
1325
```
1426

15-
## 题意
16-
> ...
27+
**Example 2:**
1728

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Count Prefix and Suffix Pairs I
23-
```go
2429
```
30+
Input: words = ["pa","papa","ma","mama"]
31+
Output: 2
32+
Explanation: In this example, the counted index pairs are:
33+
i = 0 and j = 1 because isPrefixAndSuffix("pa", "papa") is true.
34+
i = 2 and j = 3 because isPrefixAndSuffix("ma", "mama") is true.
35+
Therefore, the answer is 2.
36+
```
37+
38+
**Example 3:**
2539

40+
```
41+
Input: words = ["abab","ab"]
42+
Output: 0
43+
Explanation: In this example, the only valid index pair is i = 0 and j = 1, and isPrefixAndSuffix("abab", "ab") is false.
44+
Therefore, the answer is 0.
45+
```
2646

2747
## 结语
2848

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "strings"
4+
5+
func Solution(words []string) int {
6+
ans := 0
7+
for i := 0; i < len(words); i++ {
8+
for j := i + 1; j < len(words); j++ {
9+
if strings.HasPrefix(words[j], words[i]) && strings.HasSuffix(words[j], words[i]) {
10+
ans++
11+
}
12+
}
13+
}
14+
return ans
515
}

leetcode/3001-3100/3042.Count-Prefix-and-Suffix-Pairs-I/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []string
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []string{"a", "aba", "ababa", "aa"}, 4},
17+
{"TestCase2", []string{"pa", "papa", "ma", "mama"}, 2},
18+
{"TestCase3", []string{"abab", "ab"}, 0},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)