Skip to content

Commit ff6af44

Browse files
committed
feat: add solutions to lc problem: No.1759
No.1759.Count Number of Homogenous Substrings
1 parent 09a6701 commit ff6af44

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ class Solution:
8484
return ans
8585
```
8686

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+
8798
### **Java**
8899

89100
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -109,6 +120,22 @@ class Solution {
109120
}
110121
```
111122

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+
112139
### **C++**
113140

114141
```cpp
@@ -131,6 +158,23 @@ public:
131158
};
132159
```
133160

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+
134178
### **Go**
135179

136180
```go
@@ -150,6 +194,23 @@ func countHomogenous(s string) (ans int) {
150194
}
151195
```
152196

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+
153214
### **...**
154215

155216
```

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ class Solution:
9090
return ans
9191
```
9292

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+
93104
### **Java**
94105

95106
```java
@@ -113,6 +124,22 @@ class Solution {
113124
}
114125
```
115126

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+
116143
### **C++**
117144

118145
```cpp
@@ -135,6 +162,23 @@ public:
135162
};
136163
```
137164

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+
138182
### **Go**
139183

140184
```go
@@ -154,6 +198,23 @@ func countHomogenous(s string) (ans int) {
154198
}
155199
```
156200

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+
157218
### **...**
158219

159220
```

0 commit comments

Comments
 (0)