|
| 1 | +# [2955. Number of Same-End Substrings](https://leetcode.cn/problems/number-of-same-end-substrings) |
| 2 | + |
| 3 | +[English Version](/solution/2900-2999/2955.Number%20of%20Same-End%20Substrings/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>You are given a <strong>0-indexed</strong> string <code>s</code>, and a 2D array of integers <code>queries</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code> indicates a substring of <code>s</code> starting from the index <code>l<sub>i</sub></code> and ending at the index <code>r<sub>i</sub></code> (both <strong>inclusive</strong>), i.e. <code>s[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> |
| 10 | + |
| 11 | +<p>Return <em>an array </em><code>ans</code><em> where</em> <code>ans[i]</code> <em>is the number of <strong>same-end</strong> substrings of</em> <code>queries[i]</code>.</p> |
| 12 | + |
| 13 | +<p>A <strong>0-indexed</strong> string <code>t</code> of length <code>n</code> is called <strong>same-end</strong> if it has the same character at both of its ends, i.e., <code>t[0] == t[n - 1]</code>.</p> |
| 14 | + |
| 15 | +<p>A <b>substring</b> is a contiguous non-empty sequence of characters within a string.</p> |
| 16 | + |
| 17 | +<p> </p> |
| 18 | +<p><strong class="example">Example 1:</strong></p> |
| 19 | + |
| 20 | +<pre> |
| 21 | +<strong>Input:</strong> s = "abcaab", queries = [[0,0],[1,4],[2,5],[0,5]] |
| 22 | +<strong>Output:</strong> [1,5,5,10] |
| 23 | +<strong>Explanation:</strong> Here is the same-end substrings of each query: |
| 24 | +1<sup>st</sup> query: s[0..0] is "a" which has 1 same-end substring: "<strong><u>a</u></strong>". |
| 25 | +2<sup>nd</sup> query: s[1..4] is "bcaa" which has 5 same-end substrings: "<strong><u>b</u></strong>caa", "b<strong><u>c</u></strong>aa", "bc<strong><u>a</u></strong>a", "bca<strong><u>a</u></strong>", "bc<strong><u>aa</u></strong>". |
| 26 | +3<sup>rd</sup> query: s[2..5] is "caab" which has 5 same-end substrings: "<strong><u>c</u></strong>aab", "c<strong><u>a</u></strong>ab", "ca<strong><u>a</u></strong>b", "caa<strong><u>b</u></strong>", "c<strong><u>aa</u></strong>b". |
| 27 | +4<sup>th</sup> query: s[0..5] is "abcaab" which has 10 same-end substrings: "<strong><u>a</u></strong>bcaab", "a<strong><u>b</u></strong>caab", "ab<strong><u>c</u></strong>aab", "abc<strong><u>a</u></strong>ab", "abca<strong><u>a</u></strong>b", "abcaa<strong><u>b</u></strong>", "abc<strong><u>aa</u></strong>b", "<strong><u>abca</u></strong>ab", "<strong><u>abcaa</u></strong>b", "a<strong><u>bcaab</u></strong>". |
| 28 | +</pre> |
| 29 | + |
| 30 | +<p><strong class="example">Example 2:</strong></p> |
| 31 | + |
| 32 | +<pre> |
| 33 | +<strong>Input:</strong> s = "abcd", queries = [[0,3]] |
| 34 | +<strong>Output:</strong> [4] |
| 35 | +<strong>Explanation:</strong> The only query is s[0..3] which is "abcd". It has 4 same-end substrings: "<strong><u>a</u></strong>bcd", "a<strong><u>b</u></strong>cd", "ab<strong><u>c</u></strong>d", "abc<strong><u>d</u></strong>". |
| 36 | +</pre> |
| 37 | + |
| 38 | +<p> </p> |
| 39 | +<p><strong>Constraints:</strong></p> |
| 40 | + |
| 41 | +<ul> |
| 42 | + <li><code>2 <= s.length <= 3 * 10<sup>4</sup></code></li> |
| 43 | + <li><code>s</code> consists only of lowercase English letters.</li> |
| 44 | + <li><code>1 <= queries.length <= 3 * 10<sup>4</sup></code></li> |
| 45 | + <li><code>queries[i].length == 2</code></li> |
| 46 | + <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> |
| 47 | + <li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> < s.length</code></li> |
| 48 | +</ul> |
| 49 | + |
| 50 | +## 解法 |
| 51 | + |
| 52 | +<!-- 这里可写通用的实现逻辑 --> |
| 53 | + |
| 54 | +<!-- tabs:start --> |
| 55 | + |
| 56 | +### **Python3** |
| 57 | + |
| 58 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 59 | + |
| 60 | +```python |
| 61 | + |
| 62 | +``` |
| 63 | + |
| 64 | +### **Java** |
| 65 | + |
| 66 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 67 | + |
| 68 | +```java |
| 69 | + |
| 70 | +``` |
| 71 | + |
| 72 | +### **C++** |
| 73 | + |
| 74 | +```cpp |
| 75 | + |
| 76 | +``` |
| 77 | + |
| 78 | +### **Go** |
| 79 | + |
| 80 | +```go |
| 81 | + |
| 82 | +``` |
| 83 | + |
| 84 | +### **...** |
| 85 | + |
| 86 | +``` |
| 87 | +
|
| 88 | +``` |
| 89 | + |
| 90 | +<!-- tabs:end --> |
0 commit comments