File tree 5 files changed +147
-0
lines changed
solution/1700-1799/1759.Count Number of Homogenous Substrings
5 files changed +147
-0
lines changed Original file line number Diff line number Diff line change @@ -211,6 +211,60 @@ func countHomogenous(s string) int {
211
211
}
212
212
```
213
213
214
+ ### ** TypeScript**
215
+
216
+ ``` ts
217
+ function countHomogenous(s : string ): number {
218
+ const mod = 1e9 + 7 ;
219
+ const n = s .length ;
220
+ let ans = 0 ;
221
+ for (let i = 0 , j = 0 ; j < n ; j ++ ) {
222
+ if (s [i ] !== s [j ]) {
223
+ i = j ;
224
+ }
225
+ ans = (ans + j - i + 1 ) % mod ;
226
+ }
227
+ return ans ;
228
+ }
229
+ ```
230
+
231
+ ### ** Rust**
232
+
233
+ ``` rust
234
+ impl Solution {
235
+ pub fn count_homogenous (s : String ) -> i32 {
236
+ const MOD : usize = 1e 9 as usize + 7 ;
237
+ let s = s . as_bytes ();
238
+ let n = s . len ();
239
+ let mut ans = 0 ;
240
+ let mut i = 0 ;
241
+ for j in 0 .. n {
242
+ if s [i ] != s [j ] {
243
+ i = j ;
244
+ }
245
+ ans = (ans + j - i + 1 ) % MOD ;
246
+ }
247
+ ans as i32
248
+ }
249
+ }
250
+ ```
251
+
252
+ ### ** C**
253
+
254
+ ``` c
255
+ int countHomogenous (char * s) {
256
+ int MOD = 1e9 + 7;
257
+ int ans = 0;
258
+ for (int i = 0, j = 0; s[ j] ; j++) {
259
+ if (s[ i] != s[ j] ) {
260
+ i = j;
261
+ }
262
+ ans = (ans + j - i + 1) % MOD;
263
+ }
264
+ return ans;
265
+ }
266
+ ```
267
+
214
268
### **...**
215
269
216
270
```
Original file line number Diff line number Diff line change @@ -215,6 +215,60 @@ func countHomogenous(s string) int {
215
215
}
216
216
```
217
217
218
+ ### ** TypeScript**
219
+
220
+ ``` ts
221
+ function countHomogenous(s : string ): number {
222
+ const mod = 1e9 + 7 ;
223
+ const n = s .length ;
224
+ let ans = 0 ;
225
+ for (let i = 0 , j = 0 ; j < n ; j ++ ) {
226
+ if (s [i ] !== s [j ]) {
227
+ i = j ;
228
+ }
229
+ ans = (ans + j - i + 1 ) % mod ;
230
+ }
231
+ return ans ;
232
+ }
233
+ ```
234
+
235
+ ### ** Rust**
236
+
237
+ ``` rust
238
+ impl Solution {
239
+ pub fn count_homogenous (s : String ) -> i32 {
240
+ const MOD : usize = 1e 9 as usize + 7 ;
241
+ let s = s . as_bytes ();
242
+ let n = s . len ();
243
+ let mut ans = 0 ;
244
+ let mut i = 0 ;
245
+ for j in 0 .. n {
246
+ if s [i ] != s [j ] {
247
+ i = j ;
248
+ }
249
+ ans = (ans + j - i + 1 ) % MOD ;
250
+ }
251
+ ans as i32
252
+ }
253
+ }
254
+ ```
255
+
256
+ ### ** C**
257
+
258
+ ``` c
259
+ int countHomogenous (char * s) {
260
+ int MOD = 1e9 + 7;
261
+ int ans = 0;
262
+ for (int i = 0, j = 0; s[ j] ; j++) {
263
+ if (s[ i] != s[ j] ) {
264
+ i = j;
265
+ }
266
+ ans = (ans + j - i + 1) % MOD;
267
+ }
268
+ return ans;
269
+ }
270
+ ```
271
+
218
272
### **...**
219
273
220
274
```
Original file line number Diff line number Diff line change
1
+ int countHomogenous (char * s ) {
2
+ int MOD = 1e9 + 7 ;
3
+ int ans = 0 ;
4
+ for (int i = 0 , j = 0 ; s [j ]; j ++ ) {
5
+ if (s [i ] != s [j ]) {
6
+ i = j ;
7
+ }
8
+ ans = (ans + j - i + 1 ) % MOD ;
9
+ }
10
+ return ans ;
11
+ }
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn count_homogenous ( s : String ) -> i32 {
3
+ const MOD : usize = 1e9 as usize + 7 ;
4
+ let s = s. as_bytes ( ) ;
5
+ let n = s. len ( ) ;
6
+ let mut ans = 0 ;
7
+ let mut i = 0 ;
8
+ for j in 0 ..n {
9
+ if s[ i] != s[ j] {
10
+ i = j;
11
+ }
12
+ ans = ( ans + j - i + 1 ) % MOD ;
13
+ }
14
+ ans as i32
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ function countHomogenous ( s : string ) : number {
2
+ const mod = 1e9 + 7 ;
3
+ const n = s . length ;
4
+ let ans = 0 ;
5
+ for ( let i = 0 , j = 0 ; j < n ; j ++ ) {
6
+ if ( s [ i ] !== s [ j ] ) {
7
+ i = j ;
8
+ }
9
+ ans = ( ans + j - i + 1 ) % mod ;
10
+ }
11
+ return ans ;
12
+ }
You can’t perform that action at this time.
0 commit comments