Skip to content

Commit cee45f2

Browse files
authored
feat: add solutions to lc problem: No.3773 (#4891)
1 parent 8cf8a06 commit cee45f2

File tree

10 files changed

+423
-1
lines changed

10 files changed

+423
-1
lines changed
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3773.Maximum%20Number%20of%20Equal%20Length%20Runs/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3773. 最大等长连续字符组 🔒](https://leetcode.cn/problems/maximum-number-of-equal-length-runs)
10+
11+
[English Version](/solution/3700-3799/3773.Maximum%20Number%20of%20Equal%20Length%20Runs/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给定一个由小写英文字母组成的字符串&nbsp;<code>s</code>。</p>
18+
19+
<p><code>s</code>&nbsp;中的一个 <strong>连续字符组</strong> 是一个由无法再扩展的 <strong>相同</strong> 字符组成的 <strong><span data-keyword="substring-nonempty">子串</span></strong>。例如,<code>"hello"</code>&nbsp;中的连续字符组是&nbsp;<code>"h"</code>,<code>"e"</code>,<code>"ll"</code>&nbsp;&nbsp;<code>"o"</code>。</p>
20+
21+
<p>你可以 <strong>选择</strong>&nbsp;<code>s</code>&nbsp;&nbsp;<strong>相同</strong>&nbsp;长度的字符组。</p>
22+
23+
<p>返回一个整数,表示你可以在 <code>s</code> 中选择的最多连续字符组。</p>
24+
25+
<p>&nbsp;</p>
26+
27+
<p><strong class="example">示例 1:</strong></p>
28+
29+
<div class="example-block">
30+
<p><span class="example-io"><b>输入:</b>s = "hello"</span></p>
31+
32+
<p><span class="example-io"><b>输出:</b>3</span></p>
33+
34+
<p><strong>解释:</strong></p>
35+
36+
<p><code>s</code>&nbsp;中的连续字符组是&nbsp;<code>"h"</code>,<code>"e"</code>,<code>"ll"</code>&nbsp;&nbsp;<code>"o"</code>。你可以选择&nbsp;<code>"h"</code>,<code>"e"</code>&nbsp;&nbsp;<code>"o"</code>&nbsp;因为它们有相同的长度 1。</p>
37+
</div>
38+
39+
<p><strong class="example">示例 2:</strong></p>
40+
41+
<div class="example-block">
42+
<p><span class="example-io"><b>输入:</b>s = "aaabaaa"</span></p>
43+
44+
<p><span class="example-io"><b>输出:</b>2</span></p>
45+
46+
<p><strong>解释:</strong></p>
47+
48+
<p><code>s</code>&nbsp;中的连续字符组是&nbsp;<code>"aaa"</code>,<code>"b"</code>&nbsp;&nbsp;<code>"aaa"</code>。你可以选择&nbsp;<code>"aaa"</code>&nbsp;&nbsp;<code>"aaa"</code>&nbsp;因为它们有相同的长度 3。</p>
49+
</div>
50+
51+
<p>&nbsp;</p>
52+
53+
<p><strong>提示:</strong></p>
54+
55+
<ul>
56+
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
57+
<li><code>s</code>&nbsp;只包含小写英文字母。</li>
58+
</ul>
59+
60+
<!-- description:end -->
61+
62+
## 解法
63+
64+
<!-- solution:start -->
65+
66+
### 方法一:哈希表
67+
68+
我们可以用一个哈希表 $\textit{cnt}$ 来记录每个连续字符组长度出现的次数。遍历字符串 $s$,对于每个连续字符组,计算其长度 $m$,并将 $\textit{cnt}[m]$ 加 $1$。最后,答案即为 $\textit{cnt}$ 中的最大值。
69+
70+
时间复杂度
71+
72+
<!-- tabs:start -->
73+
74+
#### Python3
75+
76+
```python
77+
class Solution:
78+
def maxSameLengthRuns(self, s: str) -> int:
79+
cnt = Counter()
80+
for _, g in groupby(s):
81+
cnt[len(list(g))] += 1
82+
return max(cnt.values())
83+
```
84+
85+
#### Java
86+
87+
```java
88+
class Solution {
89+
public int maxSameLengthRuns(String s) {
90+
Map<Integer, Integer> cnt = new HashMap<>();
91+
int ans = 0;
92+
int n = s.length();
93+
for (int i = 0; i < n;) {
94+
int j = i + 1;
95+
while (j < n && s.charAt(j) == s.charAt(i)) {
96+
++j;
97+
}
98+
int m = j - i;
99+
ans = Math.max(ans, cnt.merge(m, 1, Integer::sum));
100+
i = j;
101+
}
102+
return ans;
103+
}
104+
}
105+
```
106+
107+
#### C++
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
int maxSameLengthRuns(string s) {
113+
unordered_map<int, int> cnt;
114+
int ans = 0;
115+
int n = s.size();
116+
for (int i = 0; i < n;) {
117+
int j = i + 1;
118+
while (j < n && s[j] == s[i]) {
119+
++j;
120+
}
121+
int m = j - i;
122+
ans = max(ans, ++cnt[m]);
123+
i = j;
124+
}
125+
return ans;
126+
}
127+
};
128+
```
129+
130+
#### Go
131+
132+
```go
133+
func maxSameLengthRuns(s string) (ans int) {
134+
cnt := map[int]int{}
135+
n := len(s)
136+
for i := 0; i < n; {
137+
j := i + 1
138+
for j < n && s[j] == s[i] {
139+
j++
140+
}
141+
m := j - i
142+
cnt[m]++
143+
ans = max(ans, cnt[m])
144+
i = j
145+
}
146+
return
147+
}
148+
```
149+
150+
#### TypeScript
151+
152+
```ts
153+
function maxSameLengthRuns(s: string): number {
154+
const cnt: Record<number, number> = {};
155+
const n = s.length;
156+
let ans = 0;
157+
for (let i = 0; i < n; ) {
158+
let j = i + 1;
159+
while (j < n && s[j] === s[i]) {
160+
++j;
161+
}
162+
const m = j - i;
163+
cnt[m] = (cnt[m] || 0) + 1;
164+
ans = Math.max(ans, cnt[m]);
165+
i = j;
166+
}
167+
return ans;
168+
}
169+
```
170+
171+
<!-- tabs:end -->
172+
173+
<!-- solution:end -->
174+
175+
<!-- problem:end -->
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3773.Maximum%20Number%20of%20Equal%20Length%20Runs/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3773. Maximum Number of Equal Length Runs 🔒](https://leetcode.com/problems/maximum-number-of-equal-length-runs)
10+
11+
[中文文档](/solution/3700-3799/3773.Maximum%20Number%20of%20Equal%20Length%20Runs/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>You are given a string <code>s</code> consisting of lowercase English letters.</p>
18+
19+
<p>A <strong>run</strong> in <code>s</code> is a <strong><span data-keyword="substring-nonempty">substring</span></strong> of <strong>equal</strong> letters that cannot be extended further. For example, the runs in <code>&quot;hello&quot;</code> are <code>&quot;h&quot;</code>, <code>&quot;e&quot;</code>, <code>&quot;ll&quot;</code>, and <code>&quot;o&quot;</code>.</p>
20+
21+
<p>You can <strong>select</strong> runs that have the <strong>same</strong> length in <code>s</code>.</p>
22+
23+
<p>Return an integer denoting the <strong>maximum</strong> number of runs you can select in <code>s</code>.</p>
24+
25+
<p>&nbsp;</p>
26+
<p><strong class="example">Example 1:</strong></p>
27+
28+
<div class="example-block">
29+
<p><strong>Input:</strong> <span class="example-io">s = &quot;hello&quot;</span></p>
30+
31+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
32+
33+
<p><strong>Explanation:</strong></p>
34+
35+
<p>The runs in <code>s</code> are <code>&quot;h&quot;</code>, <code>&quot;e&quot;</code>, <code>&quot;ll&quot;</code>, and <code>&quot;o&quot;</code>. You can select <code>&quot;h&quot;</code>, <code>&quot;e&quot;</code>, and <code>&quot;o&quot;</code> because they have the same length 1.</p>
36+
</div>
37+
38+
<p><strong class="example">Example 2:</strong></p>
39+
40+
<div class="example-block">
41+
<p><strong>Input:</strong> <span class="example-io">s = &quot;aaabaaa&quot;</span></p>
42+
43+
<p><strong>Output:</strong> <span class="example-io">2</span></p>
44+
45+
<p><strong>Explanation:</strong></p>
46+
47+
<p>The runs in <code>s</code> are <code>&quot;aaa&quot;</code>, <code>&quot;b&quot;</code>, and <code>&quot;aaa&quot;</code>. You can select <code>&quot;aaa&quot;</code> and <code>&quot;aaa&quot;</code> because they have the same length 3.</p>
48+
</div>
49+
50+
<p>&nbsp;</p>
51+
<p><strong>Constraints:</strong></p>
52+
53+
<ul>
54+
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
55+
<li><code>s</code> consists of lowercase English letters only.</li>
56+
</ul>
57+
58+
<!-- description:end -->
59+
60+
## Solutions
61+
62+
<!-- solution:start -->
63+
64+
### Solution 1: Hash Table
65+
66+
We can use a hash table $\textit{cnt}$ to record the number of occurrences of each run length. We traverse the string $s$, and for each run, we calculate its length $m$ and increment $\textit{cnt}[m]$ by $1$. Finally, the answer is the maximum value in $\textit{cnt}$.
67+
68+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string $s$.
69+
70+
<!-- tabs:start -->
71+
72+
#### Python3
73+
74+
```python
75+
class Solution:
76+
def maxSameLengthRuns(self, s: str) -> int:
77+
cnt = Counter()
78+
for _, g in groupby(s):
79+
cnt[len(list(g))] += 1
80+
return max(cnt.values())
81+
```
82+
83+
#### Java
84+
85+
```java
86+
class Solution {
87+
public int maxSameLengthRuns(String s) {
88+
Map<Integer, Integer> cnt = new HashMap<>();
89+
int ans = 0;
90+
int n = s.length();
91+
for (int i = 0; i < n;) {
92+
int j = i + 1;
93+
while (j < n && s.charAt(j) == s.charAt(i)) {
94+
++j;
95+
}
96+
int m = j - i;
97+
ans = Math.max(ans, cnt.merge(m, 1, Integer::sum));
98+
i = j;
99+
}
100+
return ans;
101+
}
102+
}
103+
```
104+
105+
#### C++
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
int maxSameLengthRuns(string s) {
111+
unordered_map<int, int> cnt;
112+
int ans = 0;
113+
int n = s.size();
114+
for (int i = 0; i < n;) {
115+
int j = i + 1;
116+
while (j < n && s[j] == s[i]) {
117+
++j;
118+
}
119+
int m = j - i;
120+
ans = max(ans, ++cnt[m]);
121+
i = j;
122+
}
123+
return ans;
124+
}
125+
};
126+
```
127+
128+
#### Go
129+
130+
```go
131+
func maxSameLengthRuns(s string) (ans int) {
132+
cnt := map[int]int{}
133+
n := len(s)
134+
for i := 0; i < n; {
135+
j := i + 1
136+
for j < n && s[j] == s[i] {
137+
j++
138+
}
139+
m := j - i
140+
cnt[m]++
141+
ans = max(ans, cnt[m])
142+
i = j
143+
}
144+
return
145+
}
146+
```
147+
148+
#### TypeScript
149+
150+
```ts
151+
function maxSameLengthRuns(s: string): number {
152+
const cnt: Record<number, number> = {};
153+
const n = s.length;
154+
let ans = 0;
155+
for (let i = 0; i < n; ) {
156+
let j = i + 1;
157+
while (j < n && s[j] === s[i]) {
158+
++j;
159+
}
160+
const m = j - i;
161+
cnt[m] = (cnt[m] || 0) + 1;
162+
ans = Math.max(ans, cnt[m]);
163+
i = j;
164+
}
165+
return ans;
166+
}
167+
```
168+
169+
<!-- tabs:end -->
170+
171+
<!-- solution:end -->
172+
173+
<!-- problem:end -->
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int maxSameLengthRuns(string s) {
4+
unordered_map<int, int> cnt;
5+
int ans = 0;
6+
int n = s.size();
7+
for (int i = 0; i < n;) {
8+
int j = i + 1;
9+
while (j < n && s[j] == s[i]) {
10+
++j;
11+
}
12+
int m = j - i;
13+
ans = max(ans, ++cnt[m]);
14+
i = j;
15+
}
16+
return ans;
17+
}
18+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func maxSameLengthRuns(s string) (ans int) {
2+
cnt := map[int]int{}
3+
n := len(s)
4+
for i := 0; i < n; {
5+
j := i + 1
6+
for j < n && s[j] == s[i] {
7+
j++
8+
}
9+
m := j - i
10+
cnt[m]++
11+
ans = max(ans, cnt[m])
12+
i = j
13+
}
14+
return
15+
}

0 commit comments

Comments
 (0)