Skip to content

Commit 6ad792c

Browse files
committed
feat: add solutions to lc problem: No.1759
No.1759.Count Number of Homogenous Substrings
1 parent 9d1c70b commit 6ad792c

File tree

8 files changed

+215
-9
lines changed

8 files changed

+215
-9
lines changed

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

+80-1
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,101 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:双指针**
57+
58+
遍历字符串 $s$,用指针 $i$ 指向当前字符,指针 $j$ 指向下一个不同的字符,那么 $[i,..j-1]$ 区间内的字符都是相同的,假设 $cnt=j-i$,那么该区间内的同构子字符串个数为 $\frac{(1 + cnt) \times cnt}{2}$,将其累加到答案中即可。继续遍历,直到指针 $i$ 到达字符串末尾。
59+
60+
遍历完字符串 $s$ 后,返回答案即可。注意答案的取模操作。
61+
62+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
63+
5664
<!-- tabs:start -->
5765

5866
### **Python3**
5967

6068
<!-- 这里可写当前语言的特殊实现逻辑 -->
6169

6270
```python
63-
71+
class Solution:
72+
def countHomogenous(self, s: str) -> int:
73+
mod = 10**9 + 7
74+
i, n = 0, len(s)
75+
ans = 0
76+
while i < n:
77+
j = i
78+
while j < n and s[j] == s[i]:
79+
j += 1
80+
cnt = j - i
81+
ans += (1 + cnt) * cnt // 2
82+
ans %= mod
83+
i = j
84+
return ans
6485
```
6586

6687
### **Java**
6788

6889
<!-- 这里可写当前语言的特殊实现逻辑 -->
6990

7091
```java
92+
class Solution {
93+
private static final int MOD = (int) 1e9 + 7;
94+
95+
public int countHomogenous(String s) {
96+
int n = s.length();
97+
long ans = 0;
98+
for (int i = 0, j = 0; i < n; i = j) {
99+
j = i;
100+
while (j < n && s.charAt(j) == s.charAt(i)) {
101+
++j;
102+
}
103+
int cnt = j - i;
104+
ans += (long) (1 + cnt) * cnt / 2;
105+
ans %= MOD;
106+
}
107+
return (int) ans;
108+
}
109+
}
110+
```
111+
112+
### **C++**
113+
114+
```cpp
115+
class Solution {
116+
public:
117+
const int mod = 1e9 + 7;
118+
119+
int countHomogenous(string s) {
120+
int n = s.size();
121+
long ans = 0;
122+
for (int i = 0, j = 0; i < n; i = j) {
123+
j = i;
124+
while (j < n && s[j] == s[i]) ++j;
125+
int cnt = j - i;
126+
ans += 1ll * (1 + cnt) * cnt / 2;
127+
ans %= mod;
128+
}
129+
return ans;
130+
}
131+
};
132+
```
71133

134+
### **Go**
135+
136+
```go
137+
func countHomogenous(s string) (ans int) {
138+
n := len(s)
139+
const mod int = 1e9 + 7
140+
for i, j := 0, 0; i < n; i = j {
141+
j = i
142+
for j < n && s[j] == s[i] {
143+
j++
144+
}
145+
cnt := j - i
146+
ans += (1 + cnt) * cnt / 2
147+
ans %= mod
148+
}
149+
return
150+
}
72151
```
73152

74153
### **...**

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

+72-4
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,8 @@
6363
<p><strong>Constraints:</strong></p>
6464

6565
<ul>
66-
6766
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
68-
6967
<li><code>s</code> consists of lowercase letters.</li>
70-
7168
</ul>
7269

7370
## Solutions
@@ -77,13 +74,84 @@
7774
### **Python3**
7875

7976
```python
80-
77+
class Solution:
78+
def countHomogenous(self, s: str) -> int:
79+
mod = 10**9 + 7
80+
i, n = 0, len(s)
81+
ans = 0
82+
while i < n:
83+
j = i
84+
while j < n and s[j] == s[i]:
85+
j += 1
86+
cnt = j - i
87+
ans += (1 + cnt) * cnt // 2
88+
ans %= mod
89+
i = j
90+
return ans
8191
```
8292

8393
### **Java**
8494

8595
```java
96+
class Solution {
97+
private static final int MOD = (int) 1e9 + 7;
98+
99+
public int countHomogenous(String s) {
100+
int n = s.length();
101+
long ans = 0;
102+
for (int i = 0, j = 0; i < n; i = j) {
103+
j = i;
104+
while (j < n && s.charAt(j) == s.charAt(i)) {
105+
++j;
106+
}
107+
int cnt = j - i;
108+
ans += (long) (1 + cnt) * cnt / 2;
109+
ans %= MOD;
110+
}
111+
return (int) ans;
112+
}
113+
}
114+
```
115+
116+
### **C++**
117+
118+
```cpp
119+
class Solution {
120+
public:
121+
const int mod = 1e9 + 7;
122+
123+
int countHomogenous(string s) {
124+
int n = s.size();
125+
long ans = 0;
126+
for (int i = 0, j = 0; i < n; i = j) {
127+
j = i;
128+
while (j < n && s[j] == s[i]) ++j;
129+
int cnt = j - i;
130+
ans += 1ll * (1 + cnt) * cnt / 2;
131+
ans %= mod;
132+
}
133+
return ans;
134+
}
135+
};
136+
```
86137

138+
### **Go**
139+
140+
```go
141+
func countHomogenous(s string) (ans int) {
142+
n := len(s)
143+
const mod int = 1e9 + 7
144+
for i, j := 0, 0; i < n; i = j {
145+
j = i
146+
for j < n && s[j] == s[i] {
147+
j++
148+
}
149+
cnt := j - i
150+
ans += (1 + cnt) * cnt / 2
151+
ans %= mod
152+
}
153+
return
154+
}
87155
```
88156

89157
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
const int mod = 1e9 + 7;
4+
5+
int countHomogenous(string s) {
6+
int n = s.size();
7+
long ans = 0;
8+
for (int i = 0, j = 0; i < n; i = j) {
9+
j = i;
10+
while (j < n && s[j] == s[i]) ++j;
11+
int cnt = j - i;
12+
ans += 1ll * (1 + cnt) * cnt / 2;
13+
ans %= mod;
14+
}
15+
return ans;
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func countHomogenous(s string) (ans int) {
2+
n := len(s)
3+
const mod int = 1e9 + 7
4+
for i, j := 0, 0; i < n; i = j {
5+
j = i
6+
for j < n && s[j] == s[i] {
7+
j++
8+
}
9+
cnt := j - i
10+
ans += (1 + cnt) * cnt / 2
11+
ans %= mod
12+
}
13+
return
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
private static final int MOD = (int) 1e9 + 7;
3+
4+
public int countHomogenous(String s) {
5+
int n = s.length();
6+
long ans = 0;
7+
for (int i = 0, j = 0; i < n; i = j) {
8+
j = i;
9+
while (j < n && s.charAt(j) == s.charAt(i)) {
10+
++j;
11+
}
12+
int cnt = j - i;
13+
ans += (long) (1 + cnt) * cnt / 2;
14+
ans %= MOD;
15+
}
16+
return (int) ans;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def countHomogenous(self, s: str) -> int:
3+
mod = 10**9 + 7
4+
i, n = 0, len(s)
5+
ans = 0
6+
while i < n:
7+
j = i
8+
while j < n and s[j] == s[i]:
9+
j += 1
10+
cnt = j - i
11+
ans += (1 + cnt) * cnt // 2
12+
ans %= mod
13+
i = j
14+
return ans

solution/1700-1799/1760.Minimum Limit of Balls in a Bag/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@
1212

1313
<ul>
1414
<li>选择任意一个袋子,并将袋子里的球分到 2 个新的袋子中,每个袋子里都有 <strong>正整数</strong> 个球。
15-
1615
<ul>
1716
<li>比方说,一个袋子里有 <code>5</code> 个球,你可以把它们分到两个新袋子里,分别有 <code>1</code> 个和 <code>4</code> 个球,或者分别有 <code>2</code> 个和 <code>3</code> 个球。</li>
1817
</ul>
1918
</li>
20-
2119
</ul>
2220

2321
<p>你的开销是单个袋子里球数目的 <strong>最大值</strong> ,你想要 <strong>最小化</strong> 开销。</p>

solution/1700-1799/1760.Minimum Limit of Balls in a Bag/README_EN.md

-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010

1111
<ul>
1212
<li>Take any bag of balls and divide it into two new bags with a <strong>positive </strong>number of balls.
13-
1413
<ul>
1514
<li>For example, a bag of <code>5</code> balls can become two new bags of <code>1</code> and <code>4</code> balls, or two new bags of <code>2</code> and <code>3</code> balls.</li>
1615
</ul>
1716
</li>
18-
1917
</ul>
2018

2119
<p>Your penalty is the <strong>maximum</strong> number of balls in a bag. You want to <strong>minimize</strong> your penalty after the operations.</p>

0 commit comments

Comments
 (0)