Skip to content

Commit ad013ab

Browse files
committed
feat: add solutions to lc problem: No.2083
No.2083.Substrings That Begin and End With the Same Letter
1 parent 2e06fc7 commit ad013ab

File tree

6 files changed

+184
-25
lines changed

6 files changed

+184
-25
lines changed

solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/README.md

Lines changed: 85 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,70 +6,131 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>You are given a <strong>0-indexed</strong> string <code>s</code> consisting of only lowercase English letters. Return <em>the number of <strong>substrings</strong> in </em><code>s</code> <em>that begin and end with the <strong>same</strong> character.</em></p>
9+
<p>给你一个仅由小写英文字母组成的,&nbsp; 下标从 <code>0</code> 开始的字符串 <code>s</code> 。返回 <code>s</code> 中以相同字符开头和结尾的子字符串总数。</p>
1010

11-
<p>A <strong>substring</strong> is a contiguous non-empty sequence of characters within a string.</p>
11+
<p>子字符串是字符串中连续的非空字符序列。</p>
1212

1313
<p>&nbsp;</p>
14-
<p><strong>Example 1:</strong></p>
14+
15+
<p><strong>示例 1:</strong></p>
1516

1617
<pre>
17-
<strong>Input:</strong> s = &quot;abcba&quot;
18-
<strong>Output:</strong> 7
19-
<strong>Explanation:</strong>
20-
The substrings of length 1 that start and end with the same letter are: &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;b&quot;, and &quot;a&quot;.
21-
The substring of length 3 that starts and ends with the same letter is: &quot;bcb&quot;.
22-
The substring of length 5 that starts and ends with the same letter is: &quot;abcba&quot;.
18+
<strong>输入:</strong>s = "abcba"
19+
<strong>输出:</strong>7
20+
<strong>解释:</strong>
21+
以相同字母开头和结尾的长度为 1 的子串是:"a"、"b"、"c"、"b" 和 "a" 。
22+
以相同字母开头和结尾的长度为 3 的子串是:"bcb" 。
23+
以相同字母开头和结尾的长度为 5 的子串是:"abcba" 。
2324
</pre>
2425

25-
<p><strong>Example 2:</strong></p>
26+
<p><strong>示例 2:</strong></p>
2627

2728
<pre>
28-
<strong>Input:</strong> s = &quot;abacad&quot;
29-
<strong>Output:</strong> 9
30-
<strong>Explanation:</strong>
31-
The substrings of length 1 that start and end with the same letter are: &quot;a&quot;, &quot;b&quot;, &quot;a&quot;, &quot;c&quot;, &quot;a&quot;, and &quot;d&quot;.
32-
The substrings of length 3 that start and end with the same letter are: &quot;aba&quot; and &quot;aca&quot;.
33-
The substring of length 5 that starts and ends with the same letter is: &quot;abaca&quot;.
29+
<strong>输入:</strong>s = "abacad"
30+
<strong>输出:</strong>9
31+
<strong>解释:</strong>
32+
以相同字母开头和结尾的长度为 1 的子串是:"a"、"b"、"a"、"c"、"a" 和 "d" 。
33+
以相同字母开头和结尾的长度为 3 的子串是:"aba" 和 "aca" 。
34+
以相同字母开头和结尾的长度为 5 的子串是:"abaca" 。
3435
</pre>
3536

36-
<p><strong>Example 3:</strong></p>
37+
<p><strong>示例 3:</strong></p>
3738

3839
<pre>
39-
<strong>Input:</strong> s = &quot;a&quot;
40-
<strong>Output:</strong> 1
41-
<strong>Explanation:</strong>
42-
The substring of length 1 that starts and ends with the same letter is: &quot;a&quot;.
40+
<strong>输入:</strong>s = "a"
41+
<strong>输出:</strong>1
42+
<strong>解释:</strong>
43+
只有一个,以相同字母开头和结尾的长度为 1 的子串是:"a"。
4344
</pre>
4445

4546
<p>&nbsp;</p>
46-
<p><strong>Constraints:</strong></p>
47+
48+
<p><strong>提示:</strong></p>
4749

4850
<ul>
4951
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
50-
<li><code>s</code> consists only of lowercase English letters.</li>
52+
<li><code>s</code> 仅包含小写英文字母。</li>
5153
</ul>
5254

5355
## 解法
5456

5557
<!-- 这里可写通用的实现逻辑 -->
5658

59+
前缀和 + 计数器。
60+
61+
用 counter 累计以每个字符结尾的字符串的个数。
62+
63+
遍历字符串 s 中每个字符 c,累加以 c 字符串结尾的字符串个数。
64+
5765
<!-- tabs:start -->
5866

5967
### **Python3**
6068

6169
<!-- 这里可写当前语言的特殊实现逻辑 -->
6270

6371
```python
64-
72+
class Solution:
73+
def numberOfSubstrings(self, s: str) -> int:
74+
counter = [0] * 26
75+
ans = 0
76+
for c in s:
77+
i = ord(c) - ord('a')
78+
counter[i] += 1
79+
ans += counter[i]
80+
return ans
6581
```
6682

6783
### **Java**
6884

6985
<!-- 这里可写当前语言的特殊实现逻辑 -->
7086

7187
```java
88+
class Solution {
89+
public long numberOfSubstrings(String s) {
90+
int[] counter = new int[26];
91+
long ans = 0;
92+
for (char c : s.toCharArray()) {
93+
int i = c - 'a';
94+
++counter[i];
95+
ans += counter[i];
96+
}
97+
return ans;
98+
}
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
long long numberOfSubstrings(string s) {
108+
vector<int> counter(26);
109+
long long ans = 0;
110+
for (char c : s)
111+
{
112+
int i = c - 'a';
113+
++counter[i];
114+
ans += counter[i];
115+
}
116+
return ans;
117+
}
118+
};
119+
```
72120
121+
### **Go**
122+
123+
```go
124+
func numberOfSubstrings(s string) int64 {
125+
var ans int64
126+
counter := make([]int64, 26)
127+
for _, c := range s {
128+
i := c - 'a'
129+
counter[i]++
130+
ans += counter[i]
131+
}
132+
return ans
133+
}
73134
```
74135

75136
### **...**

solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/README_EN.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,66 @@ The substring of length 1 that starts and ends with the same letter is: &quot;a&
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def numberOfSubstrings(self, s: str) -> int:
60+
counter = [0] * 26
61+
ans = 0
62+
for c in s:
63+
i = ord(c) - ord('a')
64+
counter[i] += 1
65+
ans += counter[i]
66+
return ans
5967
```
6068

6169
### **Java**
6270

6371
```java
72+
class Solution {
73+
public long numberOfSubstrings(String s) {
74+
int[] counter = new int[26];
75+
long ans = 0;
76+
for (char c : s.toCharArray()) {
77+
int i = c - 'a';
78+
++counter[i];
79+
ans += counter[i];
80+
}
81+
return ans;
82+
}
83+
}
84+
```
85+
86+
### **C++**
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
long long numberOfSubstrings(string s) {
92+
vector<int> counter(26);
93+
long long ans = 0;
94+
for (char c : s)
95+
{
96+
int i = c - 'a';
97+
++counter[i];
98+
ans += counter[i];
99+
}
100+
return ans;
101+
}
102+
};
103+
```
64104
105+
### **Go**
106+
107+
```go
108+
func numberOfSubstrings(s string) int64 {
109+
var ans int64
110+
counter := make([]int64, 26)
111+
for _, c := range s {
112+
i := c - 'a'
113+
counter[i]++
114+
ans += counter[i]
115+
}
116+
return ans
117+
}
65118
```
66119

67120
### **...**
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
long long numberOfSubstrings(string s) {
4+
vector<int> counter(26);
5+
long long ans = 0;
6+
for (char c : s)
7+
{
8+
int i = c - 'a';
9+
++counter[i];
10+
ans += counter[i];
11+
}
12+
return ans;
13+
}
14+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func numberOfSubstrings(s string) int64 {
2+
var ans int64
3+
counter := make([]int64, 26)
4+
for _, c := range s {
5+
i := c - 'a'
6+
counter[i]++
7+
ans += counter[i]
8+
}
9+
return ans
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public long numberOfSubstrings(String s) {
3+
int[] counter = new int[26];
4+
long ans = 0;
5+
for (char c : s.toCharArray()) {
6+
int i = c - 'a';
7+
++counter[i];
8+
ans += counter[i];
9+
}
10+
return ans;
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def numberOfSubstrings(self, s: str) -> int:
3+
counter = [0] * 26
4+
ans = 0
5+
for c in s:
6+
i = ord(c) - ord('a')
7+
counter[i] += 1
8+
ans += counter[i]
9+
return ans

0 commit comments

Comments
 (0)