Skip to content

Commit 59ce33e

Browse files
authored
feat: add solutions to lc problem: No.2083 (#4008)
No.2083.Substrings That Begin and End With the Same Letter
1 parent 8275963 commit 59ce33e

File tree

7 files changed

+155
-17
lines changed

7 files changed

+155
-17
lines changed

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

+57-6
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,13 @@ tags:
7474

7575
### 方法一:数组或哈希表
7676

77-
我们可以用数组或哈希表统计字符串中每个字母出现的次数,然后遍历字符串,对于每个字母,其出现的次数即为以该字母开头和结尾的子串的个数,将所有字母的出现次数相加即为答案
77+
我们可以用哈希表或者一个长度为 $26$ 的数组 $\textit{cnt}$ 来记录每个字符出现的次数
7878

79-
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串的长度,而 $C$ 为字符集的大小。本题中 $C = 26$。
79+
遍历字符串 $\textit{s}$,对于每个字符 $\textit{c}$,我们将 $\textit{cnt}[c]$ 的值加 $1$,然后将 $\textit{cnt}[c]$ 的值加到答案中。
80+
81+
最后返回答案即可。
82+
83+
时间复杂度 $O(n)$,其中 $n$ 是字符串 $\textit{s}$ 的长度。空间复杂度 $O(|\Sigma|)$,其中 $\Sigma$ 是字符集,这里是小写英文字母,所以 $|\Sigma|=26$。
8084

8185
<!-- tabs:start -->
8286

@@ -100,10 +104,8 @@ class Solution {
100104
public long numberOfSubstrings(String s) {
101105
int[] cnt = new int[26];
102106
long ans = 0;
103-
for (int i = 0; i < s.length(); ++i) {
104-
int j = s.charAt(i) - 'a';
105-
++cnt[j];
106-
ans += cnt[j];
107+
for (char c : s.toCharArray()) {
108+
ans += ++cnt[c - 'a'];
107109
}
108110
return ans;
109111
}
@@ -140,6 +142,55 @@ func numberOfSubstrings(s string) (ans int64) {
140142
}
141143
```
142144

145+
#### TypeScript
146+
147+
```ts
148+
function numberOfSubstrings(s: string): number {
149+
const cnt: Record<string, number> = {};
150+
let ans = 0;
151+
for (const c of s) {
152+
cnt[c] = (cnt[c] || 0) + 1;
153+
ans += cnt[c];
154+
}
155+
return ans;
156+
}
157+
```
158+
159+
#### Rust
160+
161+
```rust
162+
impl Solution {
163+
pub fn number_of_substrings(s: String) -> i64 {
164+
let mut cnt = [0; 26];
165+
let mut ans = 0_i64;
166+
for c in s.chars() {
167+
let idx = (c as u8 - b'a') as usize;
168+
cnt[idx] += 1;
169+
ans += cnt[idx];
170+
}
171+
ans
172+
}
173+
}
174+
```
175+
176+
#### JavaScript
177+
178+
```js
179+
/**
180+
* @param {string} s
181+
* @return {number}
182+
*/
183+
var numberOfSubstrings = function (s) {
184+
const cnt = {};
185+
let ans = 0;
186+
for (const c of s) {
187+
cnt[c] = (cnt[c] || 0) + 1;
188+
ans += cnt[c];
189+
}
190+
return ans;
191+
};
192+
```
193+
143194
<!-- tabs:end -->
144195

145196
<!-- solution:end -->

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

+60-5
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,15 @@ The substring of length 1 that starts and ends with the same letter is: &quot;a&
7070

7171
<!-- solution:start -->
7272

73-
### Solution 1
73+
### Solution 1: Array or Hash Table
74+
75+
We can use a hash table or an array $\textit{cnt}$ of length $26$ to record the occurrences of each character.
76+
77+
Traverse the string $\textit{s}$. For each character $\textit{c}$, increment the value of $\textit{cnt}[c]$ by $1$, and then add the value of $\textit{cnt}[c]$ to the answer.
78+
79+
Finally, return the answer.
80+
81+
The time complexity is $O(n)$, where $n$ is the length of the string $\textit{s}$. The space complexity is $O(|\Sigma|)$, where $\Sigma$ is the character set. Here, it is lowercase English letters, so $|\Sigma|=26$.
7482

7583
<!-- tabs:start -->
7684

@@ -94,10 +102,8 @@ class Solution {
94102
public long numberOfSubstrings(String s) {
95103
int[] cnt = new int[26];
96104
long ans = 0;
97-
for (int i = 0; i < s.length(); ++i) {
98-
int j = s.charAt(i) - 'a';
99-
++cnt[j];
100-
ans += cnt[j];
105+
for (char c : s.toCharArray()) {
106+
ans += ++cnt[c - 'a'];
101107
}
102108
return ans;
103109
}
@@ -134,6 +140,55 @@ func numberOfSubstrings(s string) (ans int64) {
134140
}
135141
```
136142

143+
#### TypeScript
144+
145+
```ts
146+
function numberOfSubstrings(s: string): number {
147+
const cnt: Record<string, number> = {};
148+
let ans = 0;
149+
for (const c of s) {
150+
cnt[c] = (cnt[c] || 0) + 1;
151+
ans += cnt[c];
152+
}
153+
return ans;
154+
}
155+
```
156+
157+
#### Rust
158+
159+
```rust
160+
impl Solution {
161+
pub fn number_of_substrings(s: String) -> i64 {
162+
let mut cnt = [0; 26];
163+
let mut ans = 0_i64;
164+
for c in s.chars() {
165+
let idx = (c as u8 - b'a') as usize;
166+
cnt[idx] += 1;
167+
ans += cnt[idx];
168+
}
169+
ans
170+
}
171+
}
172+
```
173+
174+
#### JavaScript
175+
176+
```js
177+
/**
178+
* @param {string} s
179+
* @return {number}
180+
*/
181+
var numberOfSubstrings = function (s) {
182+
const cnt = {};
183+
let ans = 0;
184+
for (const c of s) {
185+
cnt[c] = (cnt[c] || 0) + 1;
186+
ans += cnt[c];
187+
}
188+
return ans;
189+
};
190+
```
191+
137192
<!-- tabs:end -->
138193

139194
<!-- solution:end -->

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ class Solution {
88
}
99
return ans;
1010
}
11-
};
11+
};

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ class Solution {
22
public long numberOfSubstrings(String s) {
33
int[] cnt = new int[26];
44
long ans = 0;
5-
for (int i = 0; i < s.length(); ++i) {
6-
int j = s.charAt(i) - 'a';
7-
++cnt[j];
8-
ans += cnt[j];
5+
for (char c : s.toCharArray()) {
6+
ans += ++cnt[c - 'a'];
97
}
108
return ans;
119
}
12-
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var numberOfSubstrings = function (s) {
6+
const cnt = {};
7+
let ans = 0;
8+
for (const c of s) {
9+
cnt[c] = (cnt[c] || 0) + 1;
10+
ans += cnt[c];
11+
}
12+
return ans;
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
impl Solution {
2+
pub fn number_of_substrings(s: String) -> i64 {
3+
let mut cnt = [0; 26];
4+
let mut ans = 0_i64;
5+
for c in s.chars() {
6+
let idx = (c as u8 - b'a') as usize;
7+
cnt[idx] += 1;
8+
ans += cnt[idx];
9+
}
10+
ans
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function numberOfSubstrings(s: string): number {
2+
const cnt: Record<string, number> = {};
3+
let ans = 0;
4+
for (const c of s) {
5+
cnt[c] = (cnt[c] || 0) + 1;
6+
ans += cnt[c];
7+
}
8+
return ans;
9+
}

0 commit comments

Comments
 (0)