Skip to content

Commit fc96607

Browse files
committed
feat: add new lc problem
1 parent efcefe3 commit fc96607

File tree

7 files changed

+182
-3
lines changed

7 files changed

+182
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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>&nbsp;</p>
18+
<p><strong class="example">Example 1:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> s = &quot;abcaab&quot;, 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 &quot;a&quot; which has 1 same-end substring: &quot;<strong><u>a</u></strong>&quot;.
25+
2<sup>nd</sup> query: s[1..4] is &quot;bcaa&quot; which has 5 same-end substrings: &quot;<strong><u>b</u></strong>caa&quot;, &quot;b<strong><u>c</u></strong>aa&quot;, &quot;bc<strong><u>a</u></strong>a&quot;, &quot;bca<strong><u>a</u></strong>&quot;, &quot;bc<strong><u>aa</u></strong>&quot;.
26+
3<sup>rd</sup> query: s[2..5] is &quot;caab&quot; which has 5 same-end substrings: &quot;<strong><u>c</u></strong>aab&quot;, &quot;c<strong><u>a</u></strong>ab&quot;, &quot;ca<strong><u>a</u></strong>b&quot;, &quot;caa<strong><u>b</u></strong>&quot;, &quot;c<strong><u>aa</u></strong>b&quot;.
27+
4<sup>th</sup> query: s[0..5] is &quot;abcaab&quot; which has 10 same-end substrings: &quot;<strong><u>a</u></strong>bcaab&quot;, &quot;a<strong><u>b</u></strong>caab&quot;, &quot;ab<strong><u>c</u></strong>aab&quot;, &quot;abc<strong><u>a</u></strong>ab&quot;, &quot;abca<strong><u>a</u></strong>b&quot;, &quot;abcaa<strong><u>b</u></strong>&quot;, &quot;abc<strong><u>aa</u></strong>b&quot;, &quot;<strong><u>abca</u></strong>ab&quot;, &quot;<strong><u>abcaa</u></strong>b&quot;, &quot;a<strong><u>bcaab</u></strong>&quot;.
28+
</pre>
29+
30+
<p><strong class="example">Example 2:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> s = &quot;abcd&quot;, queries = [[0,3]]
34+
<strong>Output:</strong> [4]
35+
<strong>Explanation:</strong> The only query is s[0..3] which is &quot;abcd&quot;. It has 4 same-end substrings: &quot;<strong><u>a</u></strong>bcd&quot;, &quot;a<strong><u>b</u></strong>cd&quot;, &quot;ab<strong><u>c</u></strong>d&quot;, &quot;abc<strong><u>d</u></strong>&quot;.
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
<p><strong>Constraints:</strong></p>
40+
41+
<ul>
42+
<li><code>2 &lt;= s.length &lt;= 3 * 10<sup>4</sup></code></li>
43+
<li><code>s</code> consists only of lowercase English letters.</li>
44+
<li><code>1 &lt;= queries.length &lt;= 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 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt; 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 -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# [2955. Number of Same-End Substrings](https://leetcode.com/problems/number-of-same-end-substrings)
2+
3+
[中文文档](/solution/2900-2999/2955.Number%20of%20Same-End%20Substrings/README.md)
4+
5+
## Description
6+
7+
<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>
8+
9+
<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>
10+
11+
<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>
12+
13+
<p>A <b>substring</b> is a contiguous non-empty sequence of characters within a string.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> s = &quot;abcaab&quot;, queries = [[0,0],[1,4],[2,5],[0,5]]
20+
<strong>Output:</strong> [1,5,5,10]
21+
<strong>Explanation:</strong> Here is the same-end substrings of each query:
22+
1<sup>st</sup> query: s[0..0] is &quot;a&quot; which has 1 same-end substring: &quot;<strong><u>a</u></strong>&quot;.
23+
2<sup>nd</sup> query: s[1..4] is &quot;bcaa&quot; which has 5 same-end substrings: &quot;<strong><u>b</u></strong>caa&quot;, &quot;b<strong><u>c</u></strong>aa&quot;, &quot;bc<strong><u>a</u></strong>a&quot;, &quot;bca<strong><u>a</u></strong>&quot;, &quot;bc<strong><u>aa</u></strong>&quot;.
24+
3<sup>rd</sup> query: s[2..5] is &quot;caab&quot; which has 5 same-end substrings: &quot;<strong><u>c</u></strong>aab&quot;, &quot;c<strong><u>a</u></strong>ab&quot;, &quot;ca<strong><u>a</u></strong>b&quot;, &quot;caa<strong><u>b</u></strong>&quot;, &quot;c<strong><u>aa</u></strong>b&quot;.
25+
4<sup>th</sup> query: s[0..5] is &quot;abcaab&quot; which has 10 same-end substrings: &quot;<strong><u>a</u></strong>bcaab&quot;, &quot;a<strong><u>b</u></strong>caab&quot;, &quot;ab<strong><u>c</u></strong>aab&quot;, &quot;abc<strong><u>a</u></strong>ab&quot;, &quot;abca<strong><u>a</u></strong>b&quot;, &quot;abcaa<strong><u>b</u></strong>&quot;, &quot;abc<strong><u>aa</u></strong>b&quot;, &quot;<strong><u>abca</u></strong>ab&quot;, &quot;<strong><u>abcaa</u></strong>b&quot;, &quot;a<strong><u>bcaab</u></strong>&quot;.
26+
</pre>
27+
28+
<p><strong class="example">Example 2:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> s = &quot;abcd&quot;, queries = [[0,3]]
32+
<strong>Output:</strong> [4]
33+
<strong>Explanation:</strong> The only query is s[0..3] which is &quot;abcd&quot;. It has 4 same-end substrings: &quot;<strong><u>a</u></strong>bcd&quot;, &quot;a<strong><u>b</u></strong>cd&quot;, &quot;ab<strong><u>c</u></strong>d&quot;, &quot;abc<strong><u>d</u></strong>&quot;.
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>2 &lt;= s.length &lt;= 3 * 10<sup>4</sup></code></li>
41+
<li><code>s</code> consists only of lowercase English letters.</li>
42+
<li><code>1 &lt;= queries.length &lt;= 3 * 10<sup>4</sup></code></li>
43+
<li><code>queries[i].length == 2</code></li>
44+
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
45+
<li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt; s.length</code></li>
46+
</ul>
47+
48+
## Solutions
49+
50+
<!-- tabs:start -->
51+
52+
### **Python3**
53+
54+
```python
55+
56+
```
57+
58+
### **Java**
59+
60+
```java
61+
62+
```
63+
64+
### **C++**
65+
66+
```cpp
67+
68+
```
69+
70+
### **Go**
71+
72+
```go
73+
74+
```
75+
76+
### **...**
77+
78+
```
79+
80+
```
81+
82+
<!-- tabs:end -->

solution/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -2965,6 +2965,7 @@
29652965
| 2952 | [需要添加的硬币的最小数量](/solution/2900-2999/2952.Minimum%20Number%20of%20Coins%20to%20be%20Added/README.md) | | 中等 | 第 374 场周赛 |
29662966
| 2953 | [统计完全子字符串](/solution/2900-2999/2953.Count%20Complete%20Substrings/README.md) | | 中等 | 第 374 场周赛 |
29672967
| 2954 | [统计感冒序列的数目](/solution/2900-2999/2954.Count%20the%20Number%20of%20Infection%20Sequences/README.md) | | 困难 | 第 374 场周赛 |
2968+
| 2955 | [Number of Same-End Substrings](/solution/2900-2999/2955.Number%20of%20Same-End%20Substrings/README.md) | | 中等 | 🔒 |
29682969

29692970
## 版权
29702971

solution/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -2963,6 +2963,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
29632963
| 2952 | [Minimum Number of Coins to be Added](/solution/2900-2999/2952.Minimum%20Number%20of%20Coins%20to%20be%20Added/README_EN.md) | | Medium | Weekly Contest 374 |
29642964
| 2953 | [Count Complete Substrings](/solution/2900-2999/2953.Count%20Complete%20Substrings/README_EN.md) | | Medium | Weekly Contest 374 |
29652965
| 2954 | [Count the Number of Infection Sequences](/solution/2900-2999/2954.Count%20the%20Number%20of%20Infection%20Sequences/README_EN.md) | | Hard | Weekly Contest 374 |
2966+
| 2955 | [Number of Same-End Substrings](/solution/2900-2999/2955.Number%20of%20Same-End%20Substrings/README_EN.md) | | Medium | 🔒 |
29662967

29672968
## Copyright
29682969

solution/main.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def get_all_questions(self, retry: int = 3) -> List:
5050
)
5151
return resp.json()["stat_status_pairs"]
5252
except Exception as e:
53-
print(e)
53+
print('get_all_questions', e)
5454
time.sleep(2)
5555
return self.get_all_questions(retry - 1) if retry > 0 else []
5656

@@ -100,12 +100,15 @@ def get_question_detail_en(self, question_title_slug: str, retry: int = 3) -> di
100100
res = resp.json()
101101
return res["data"]["question"] or {}
102102
except Exception as e:
103-
print(e)
103+
print('get_question_detail_en', e)
104+
if 'is not defined' in str(e):
105+
return {}
104106
time.sleep(2)
105107
return {}
106108

107109
def get_question_detail(self, question_title_slug: str, retry: int = 3) -> dict:
108110
"""获取题目详情"""
111+
print(question_title_slug)
109112
form1 = {
110113
"operationName": "globalData",
111114
"query": "query globalData {\n feature {\n questionTranslation\n subscription\n signUp\n "
@@ -170,7 +173,7 @@ def get_question_detail(self, question_title_slug: str, retry: int = 3) -> dict:
170173
res = resp.json()
171174
return res["data"]["question"] or {}
172175
except Exception as e:
173-
print(e)
176+
print('get_question_detail', e)
174177
time.sleep(2)
175178
return {}
176179

solution/summary.md

+1
Original file line numberDiff line numberDiff line change
@@ -3012,3 +3012,4 @@
30123012
- [2952.需要添加的硬币的最小数量](/solution/2900-2999/2952.Minimum%20Number%20of%20Coins%20to%20be%20Added/README.md)
30133013
- [2953.统计完全子字符串](/solution/2900-2999/2953.Count%20Complete%20Substrings/README.md)
30143014
- [2954.统计感冒序列的数目](/solution/2900-2999/2954.Count%20the%20Number%20of%20Infection%20Sequences/README.md)
3015+
- [2955.Number of Same-End Substrings](/solution/2900-2999/2955.Number%20of%20Same-End%20Substrings/README.md)

solution/summary_en.md

+1
Original file line numberDiff line numberDiff line change
@@ -3012,3 +3012,4 @@
30123012
- [2952.Minimum Number of Coins to be Added](/solution/2900-2999/2952.Minimum%20Number%20of%20Coins%20to%20be%20Added/README_EN.md)
30133013
- [2953.Count Complete Substrings](/solution/2900-2999/2953.Count%20Complete%20Substrings/README_EN.md)
30143014
- [2954.Count the Number of Infection Sequences](/solution/2900-2999/2954.Count%20the%20Number%20of%20Infection%20Sequences/README_EN.md)
3015+
- [2955.Number of Same-End Substrings](/solution/2900-2999/2955.Number%20of%20Same-End%20Substrings/README_EN.md)

0 commit comments

Comments
 (0)