File tree Expand file tree Collapse file tree 2 files changed +122
-0
lines changed
solution/1700-1799/1759.Count Number of Homogenous Substrings Expand file tree Collapse file tree 2 files changed +122
-0
lines changed Original file line number Diff line number Diff line change @@ -84,6 +84,17 @@ class Solution:
84
84
return ans
85
85
```
86
86
87
+ ``` python
88
+ class Solution :
89
+ def countHomogenous (self , s : str ) -> int :
90
+ mod = 10 ** 9 + 7
91
+ ans = cnt = 1
92
+ for a, b in pairwise(s):
93
+ cnt = cnt + 1 if a == b else 1
94
+ ans = (ans + cnt) % mod
95
+ return ans
96
+ ```
97
+
87
98
### ** Java**
88
99
89
100
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -109,6 +120,22 @@ class Solution {
109
120
}
110
121
```
111
122
123
+ ``` java
124
+ class Solution {
125
+ private static final int MOD = (int ) 1e9 + 7 ;
126
+
127
+ public int countHomogenous (String s ) {
128
+ int n = s. length();
129
+ int ans = 1 , cnt = 1 ;
130
+ for (int i = 1 ; i < n; ++ i) {
131
+ cnt = s. charAt(i) == s. charAt(i - 1 ) ? cnt + 1 : 1 ;
132
+ ans = (ans + cnt) % MOD ;
133
+ }
134
+ return ans;
135
+ }
136
+ }
137
+ ```
138
+
112
139
### ** C++**
113
140
114
141
``` cpp
@@ -131,6 +158,23 @@ public:
131
158
};
132
159
```
133
160
161
+ ``` cpp
162
+ class Solution {
163
+ public:
164
+ const int mod = 1e9 + 7;
165
+
166
+ int countHomogenous(string s) {
167
+ int n = s.size();
168
+ int ans = 1, cnt = 1;
169
+ for (int i = 1; i < n; ++i) {
170
+ cnt = s[i] == s[i - 1] ? cnt + 1 : 1;
171
+ ans = (ans + cnt) % mod;
172
+ }
173
+ return ans;
174
+ }
175
+ };
176
+ ```
177
+
134
178
### ** Go**
135
179
136
180
``` go
@@ -150,6 +194,23 @@ func countHomogenous(s string) (ans int) {
150
194
}
151
195
```
152
196
197
+ ``` go
198
+ func countHomogenous (s string ) int {
199
+ n := len (s)
200
+ const mod int = 1e9 + 7
201
+ ans , cnt := 1 , 1
202
+ for i := 1 ; i < n; i++ {
203
+ if s[i] == s[i-1 ] {
204
+ cnt++
205
+ } else {
206
+ cnt = 1
207
+ }
208
+ ans = (ans + cnt) % mod
209
+ }
210
+ return ans
211
+ }
212
+ ```
213
+
153
214
### ** ...**
154
215
155
216
```
Original file line number Diff line number Diff line change @@ -90,6 +90,17 @@ class Solution:
90
90
return ans
91
91
```
92
92
93
+ ``` python
94
+ class Solution :
95
+ def countHomogenous (self , s : str ) -> int :
96
+ mod = 10 ** 9 + 7
97
+ ans = cnt = 1
98
+ for a, b in pairwise(s):
99
+ cnt = cnt + 1 if a == b else 1
100
+ ans = (ans + cnt) % mod
101
+ return ans
102
+ ```
103
+
93
104
### ** Java**
94
105
95
106
``` java
@@ -113,6 +124,22 @@ class Solution {
113
124
}
114
125
```
115
126
127
+ ``` java
128
+ class Solution {
129
+ private static final int MOD = (int ) 1e9 + 7 ;
130
+
131
+ public int countHomogenous (String s ) {
132
+ int n = s. length();
133
+ int ans = 1 , cnt = 1 ;
134
+ for (int i = 1 ; i < n; ++ i) {
135
+ cnt = s. charAt(i) == s. charAt(i - 1 ) ? cnt + 1 : 1 ;
136
+ ans = (ans + cnt) % MOD ;
137
+ }
138
+ return ans;
139
+ }
140
+ }
141
+ ```
142
+
116
143
### ** C++**
117
144
118
145
``` cpp
@@ -135,6 +162,23 @@ public:
135
162
};
136
163
```
137
164
165
+ ``` cpp
166
+ class Solution {
167
+ public:
168
+ const int mod = 1e9 + 7;
169
+
170
+ int countHomogenous(string s) {
171
+ int n = s.size();
172
+ int ans = 1, cnt = 1;
173
+ for (int i = 1; i < n; ++i) {
174
+ cnt = s[i] == s[i - 1] ? cnt + 1 : 1;
175
+ ans = (ans + cnt) % mod;
176
+ }
177
+ return ans;
178
+ }
179
+ };
180
+ ```
181
+
138
182
### ** Go**
139
183
140
184
``` go
@@ -154,6 +198,23 @@ func countHomogenous(s string) (ans int) {
154
198
}
155
199
```
156
200
201
+ ``` go
202
+ func countHomogenous (s string ) int {
203
+ n := len (s)
204
+ const mod int = 1e9 + 7
205
+ ans , cnt := 1 , 1
206
+ for i := 1 ; i < n; i++ {
207
+ if s[i] == s[i-1 ] {
208
+ cnt++
209
+ } else {
210
+ cnt = 1
211
+ }
212
+ ans = (ans + cnt) % mod
213
+ }
214
+ return ans
215
+ }
216
+ ```
217
+
157
218
### ** ...**
158
219
159
220
```
You can’t perform that action at this time.
0 commit comments