File tree Expand file tree Collapse file tree 6 files changed +51
-63
lines changed
solution/2000-2099/2083.Substrings That Begin and End With the Same Letter Expand file tree Collapse file tree 6 files changed +51
-63
lines changed Original file line number Diff line number Diff line change 56
56
57
57
<!-- 这里可写通用的实现逻辑 -->
58
58
59
- 前缀和 + 计数器。
59
+ ** 方法一:数组或哈希表 **
60
60
61
- 用 counter 累计以每个字符结尾的字符串的个数 。
61
+ 我们可以用数组或哈希表统计字符串中每个字母出现的次数,然后遍历字符串,对于每个字母,其出现的次数即为以该字母开头和结尾的子串的个数,将所有字母的出现次数相加即为答案 。
62
62
63
- 遍历字符串 s 中每个字符 c,累加以 c 字符串结尾的字符串个数 。
63
+ 时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串的长度,而 $C$ 为字符集的大小。本题中 $C = 26$ 。
64
64
65
65
<!-- tabs:start -->
66
66
71
71
``` python
72
72
class Solution :
73
73
def numberOfSubstrings (self , s : str ) -> int :
74
- counter = [ 0 ] * 26
74
+ cnt = Counter()
75
75
ans = 0
76
76
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]
80
79
return ans
81
80
```
82
81
@@ -87,12 +86,12 @@ class Solution:
87
86
``` java
88
87
class Solution {
89
88
public long numberOfSubstrings (String s ) {
90
- int [] counter = new int [26 ];
89
+ int [] cnt = new int [26 ];
91
90
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 ];
96
95
}
97
96
return ans;
98
97
}
@@ -105,12 +104,10 @@ class Solution {
105
104
class Solution {
106
105
public:
107
106
long long numberOfSubstrings(string s) {
108
- vector< int > counter(26) ;
107
+ int cnt [ 26 ] {} ;
109
108
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'] ;
114
111
}
115
112
return ans;
116
113
}
@@ -120,13 +117,12 @@ public:
120
117
### **Go**
121
118
122
119
```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{}
126
122
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])
130
126
}
131
127
return ans
132
128
}
Original file line number Diff line number Diff line change @@ -57,12 +57,11 @@ The substring of length 1 that starts and ends with the same letter is: "a&
57
57
``` python
58
58
class Solution :
59
59
def numberOfSubstrings (self , s : str ) -> int :
60
- counter = [ 0 ] * 26
60
+ cnt = Counter()
61
61
ans = 0
62
62
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]
66
65
return ans
67
66
```
68
67
@@ -71,12 +70,12 @@ class Solution:
71
70
``` java
72
71
class Solution {
73
72
public long numberOfSubstrings (String s ) {
74
- int [] counter = new int [26 ];
73
+ int [] cnt = new int [26 ];
75
74
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 ];
80
79
}
81
80
return ans;
82
81
}
@@ -89,12 +88,10 @@ class Solution {
89
88
class Solution {
90
89
public:
91
90
long long numberOfSubstrings(string s) {
92
- vector< int > counter(26) ;
91
+ int cnt [ 26 ] {} ;
93
92
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'] ;
98
95
}
99
96
return ans;
100
97
}
@@ -104,13 +101,12 @@ public:
104
101
### **Go**
105
102
106
103
```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{}
110
106
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])
114
110
}
115
111
return ans
116
112
}
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
3
long long numberOfSubstrings (string s) {
4
- vector< int > counter ( 26 ) ;
4
+ int cnt[ 26 ]{} ;
5
5
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' ];
10
8
}
11
9
return ans;
12
10
}
Original file line number Diff line number Diff line change 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 {}
4
3
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 ])
8
7
}
9
8
return ans
10
9
}
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public long numberOfSubstrings (String s ) {
3
- int [] counter = new int [26 ];
3
+ int [] cnt = new int [26 ];
4
4
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 ];
9
9
}
10
10
return ans ;
11
11
}
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def numberOfSubstrings (self , s : str ) -> int :
3
- counter = [ 0 ] * 26
3
+ cnt = Counter ()
4
4
ans = 0
5
5
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 ]
9
8
return ans
You can’t perform that action at this time.
0 commit comments