Skip to content

Commit 9350e85

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

File tree

6 files changed

+51
-63
lines changed

6 files changed

+51
-63
lines changed

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

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59-
前缀和 + 计数器。
59+
**方法一:数组或哈希表**
6060

61-
用 counter 累计以每个字符结尾的字符串的个数
61+
我们可以用数组或哈希表统计字符串中每个字母出现的次数,然后遍历字符串,对于每个字母,其出现的次数即为以该字母开头和结尾的子串的个数,将所有字母的出现次数相加即为答案
6262

63-
遍历字符串 s 中每个字符 c,累加以 c 字符串结尾的字符串个数
63+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串的长度,而 $C$ 为字符集的大小。本题中 $C = 26$
6464

6565
<!-- tabs:start -->
6666

@@ -71,12 +71,11 @@
7171
```python
7272
class Solution:
7373
def numberOfSubstrings(self, s: str) -> int:
74-
counter = [0] * 26
74+
cnt = Counter()
7575
ans = 0
7676
for c in s:
77-
i = ord(c) - ord('a')
78-
counter[i] += 1
79-
ans += counter[i]
77+
cnt[c] += 1
78+
ans += cnt[c]
8079
return ans
8180
```
8281

@@ -87,12 +86,12 @@ class Solution:
8786
```java
8887
class Solution {
8988
public long numberOfSubstrings(String s) {
90-
int[] counter = new int[26];
89+
int[] cnt = new int[26];
9190
long ans = 0;
92-
for (char c : s.toCharArray()) {
93-
int i = c - 'a';
94-
++counter[i];
95-
ans += counter[i];
91+
for (int i = 0; i < s.length(); ++i) {
92+
int j = s.charAt(i) - 'a';
93+
++cnt[j];
94+
ans += cnt[j];
9695
}
9796
return ans;
9897
}
@@ -105,12 +104,10 @@ class Solution {
105104
class Solution {
106105
public:
107106
long long numberOfSubstrings(string s) {
108-
vector<int> counter(26);
107+
int cnt[26]{};
109108
long long ans = 0;
110-
for (char c : s) {
111-
int i = c - 'a';
112-
++counter[i];
113-
ans += counter[i];
109+
for (char& c : s) {
110+
ans += ++cnt[c - 'a'];
114111
}
115112
return ans;
116113
}
@@ -120,13 +117,12 @@ public:
120117
### **Go**
121118
122119
```go
123-
func numberOfSubstrings(s string) int64 {
124-
var ans int64
125-
counter := make([]int64, 26)
120+
func numberOfSubstrings(s string) (ans int64) {
121+
cnt := [26]int{}
126122
for _, c := range s {
127-
i := c - 'a'
128-
counter[i]++
129-
ans += counter[i]
123+
c -= 'a'
124+
cnt[c]++
125+
ans += int64(cnt[c])
130126
}
131127
return ans
132128
}

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

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,11 @@ The substring of length 1 that starts and ends with the same letter is: &quot;a&
5757
```python
5858
class Solution:
5959
def numberOfSubstrings(self, s: str) -> int:
60-
counter = [0] * 26
60+
cnt = Counter()
6161
ans = 0
6262
for c in s:
63-
i = ord(c) - ord('a')
64-
counter[i] += 1
65-
ans += counter[i]
63+
cnt[c] += 1
64+
ans += cnt[c]
6665
return ans
6766
```
6867

@@ -71,12 +70,12 @@ class Solution:
7170
```java
7271
class Solution {
7372
public long numberOfSubstrings(String s) {
74-
int[] counter = new int[26];
73+
int[] cnt = new int[26];
7574
long ans = 0;
76-
for (char c : s.toCharArray()) {
77-
int i = c - 'a';
78-
++counter[i];
79-
ans += counter[i];
75+
for (int i = 0; i < s.length(); ++i) {
76+
int j = s.charAt(i) - 'a';
77+
++cnt[j];
78+
ans += cnt[j];
8079
}
8180
return ans;
8281
}
@@ -89,12 +88,10 @@ class Solution {
8988
class Solution {
9089
public:
9190
long long numberOfSubstrings(string s) {
92-
vector<int> counter(26);
91+
int cnt[26]{};
9392
long long ans = 0;
94-
for (char c : s) {
95-
int i = c - 'a';
96-
++counter[i];
97-
ans += counter[i];
93+
for (char& c : s) {
94+
ans += ++cnt[c - 'a'];
9895
}
9996
return ans;
10097
}
@@ -104,13 +101,12 @@ public:
104101
### **Go**
105102
106103
```go
107-
func numberOfSubstrings(s string) int64 {
108-
var ans int64
109-
counter := make([]int64, 26)
104+
func numberOfSubstrings(s string) (ans int64) {
105+
cnt := [26]int{}
110106
for _, c := range s {
111-
i := c - 'a'
112-
counter[i]++
113-
ans += counter[i]
107+
c -= 'a'
108+
cnt[c]++
109+
ans += int64(cnt[c])
114110
}
115111
return ans
116112
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
class Solution {
22
public:
33
long long numberOfSubstrings(string s) {
4-
vector<int> counter(26);
4+
int cnt[26]{};
55
long long ans = 0;
6-
for (char c : s) {
7-
int i = c - 'a';
8-
++counter[i];
9-
ans += counter[i];
6+
for (char& c : s) {
7+
ans += ++cnt[c - 'a'];
108
}
119
return ans;
1210
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
func numberOfSubstrings(s string) int64 {
2-
var ans int64
3-
counter := make([]int64, 26)
1+
func numberOfSubstrings(s string) (ans int64) {
2+
cnt := [26]int{}
43
for _, c := range s {
5-
i := c - 'a'
6-
counter[i]++
7-
ans += counter[i]
4+
c -= 'a'
5+
cnt[c]++
6+
ans += int64(cnt[c])
87
}
98
return ans
109
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
22
public long numberOfSubstrings(String s) {
3-
int[] counter = new int[26];
3+
int[] cnt = new int[26];
44
long ans = 0;
5-
for (char c : s.toCharArray()) {
6-
int i = c - 'a';
7-
++counter[i];
8-
ans += counter[i];
5+
for (int i = 0; i < s.length(); ++i) {
6+
int j = s.charAt(i) - 'a';
7+
++cnt[j];
8+
ans += cnt[j];
99
}
1010
return ans;
1111
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
class Solution:
22
def numberOfSubstrings(self, s: str) -> int:
3-
counter = [0] * 26
3+
cnt = Counter()
44
ans = 0
55
for c in s:
6-
i = ord(c) - ord('a')
7-
counter[i] += 1
8-
ans += counter[i]
6+
cnt[c] += 1
7+
ans += cnt[c]
98
return ans

0 commit comments

Comments
 (0)