Skip to content

Commit 01e0909

Browse files
committed
feat: add solutions to lc problem: No.1759
No.1759.Count Number of Homogenous Substrings
1 parent 7041489 commit 01e0909

File tree

5 files changed

+147
-0
lines changed

5 files changed

+147
-0
lines changed

solution/1700-1799/1759.Count Number of Homogenous Substrings/README.md

+54
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,60 @@ func countHomogenous(s string) int {
211211
}
212212
```
213213

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 = 1e9 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+
214268
### **...**
215269
216270
```

solution/1700-1799/1759.Count Number of Homogenous Substrings/README_EN.md

+54
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,60 @@ func countHomogenous(s string) int {
215215
}
216216
```
217217

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 = 1e9 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+
218272
### **...**
219273
220274
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}

0 commit comments

Comments
 (0)