|
1 | 1 | # [3042.Count Prefix and Suffix Pairs I][title] |
2 | 2 |
|
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 | | -
|
6 | 3 | ## 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`. |
7 | 13 |
|
8 | 14 | **Example 1:** |
9 | 15 |
|
10 | 16 | ``` |
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. |
13 | 25 | ``` |
14 | 26 |
|
15 | | -## 题意 |
16 | | -> ... |
| 27 | +**Example 2:** |
17 | 28 |
|
18 | | -## 题解 |
19 | | - |
20 | | -### 思路1 |
21 | | -> ... |
22 | | -Count Prefix and Suffix Pairs I |
23 | | -```go |
24 | 29 | ``` |
| 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:** |
25 | 39 |
|
| 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 | +``` |
26 | 46 |
|
27 | 47 | ## 结语 |
28 | 48 |
|
|
0 commit comments