Skip to content

Commit e212e5c

Browse files
committed
+ problem 395
1 parent 3070808 commit e212e5c

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# 395. Longest Substring with At Least K Repeating Characters
2+
Given a string `s` and an integer `k`, return *the length of the longest substring of* `s` *such that the frequency of each character in this substring is greater than or equal to* `k`.
3+
4+
if no such substring exists, return 0.
5+
6+
#### Example 1:
7+
<pre>
8+
<strong>Input:</strong> s = "aaabb", k = 3
9+
<strong>Output:</strong> 3
10+
<strong>Explanation:</strong> The longest substring is "aaa", as 'a' is repeated 3 times.
11+
</pre>
12+
13+
#### Example 2:
14+
<pre>
15+
<strong>Input:</strong> s = "ababbc", k = 2
16+
<strong>Output:</strong> 5
17+
<strong>Explanation:</strong> The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.
18+
</pre>
19+
20+
#### Constraints:
21+
* <code>1 <= s.length <= 10<sup>4</sup></code>
22+
* `s` consists of only lowercase English letters.
23+
* <code>1 <= k <= 10<sup>5</sup></code>
24+
25+
## Solutions (Rust)
26+
27+
### 1. Solution
28+
```Rust
29+
impl Solution {
30+
pub fn longest_substring(s: String, k: i32) -> i32 {
31+
Self::dfs(s.as_bytes(), 0, s.len() - 1, k as usize)
32+
}
33+
34+
fn dfs(s: &[u8], l: usize, r: usize, k: usize) -> i32 {
35+
if l > r || r - l + 1 < k {
36+
return 0;
37+
}
38+
39+
let mut indices = vec![vec![]; 26];
40+
let mut ret = (r - l + 1) as i32;
41+
42+
for i in l..=r {
43+
indices[(s[i] - b'a') as usize].push(i);
44+
}
45+
46+
for i in 0..26 {
47+
if !indices[i].is_empty() && indices[i].len() < k {
48+
ret = 0;
49+
ret = ret.max(Self::dfs(s, l, indices[i][0].saturating_sub(1), k));
50+
for j in 1..indices[i].len() {
51+
ret = ret.max(Self::dfs(s, indices[i][j - 1] + 1, indices[i][j] - 1, k));
52+
}
53+
ret = ret.max(Self::dfs(s, *indices[i].last().unwrap() + 1, r, k));
54+
break;
55+
}
56+
}
57+
58+
ret
59+
}
60+
}
61+
```
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# 395. 至少有 K 个重复字符的最长子串
2+
给你一个字符串 `s` 和一个整数 `k` ,请你找出 `s` 中的最长子串, 要求该子串中的每一字符出现次数都不少于 `k` 。返回这一子串的长度。
3+
4+
如果不存在这样的子字符串,则返回 0。
5+
6+
#### 示例 1:
7+
<pre>
8+
<strong>输入:</strong> s = "aaabb", k = 3
9+
<strong>输出:</strong> 3
10+
<strong>解释:</strong> 最长子串为 "aaa" ,其中 'a' 重复了 3 次。
11+
</pre>
12+
13+
#### 示例 2:
14+
<pre>
15+
<strong>输入:</strong> s = "ababbc", k = 2
16+
<strong>输出:</strong> 5
17+
<strong>解释:</strong> 最长子串为 "ababb" ,其中 'a' 重复了 2 次, 'b' 重复了 3 次。
18+
</pre>
19+
20+
#### 提示:
21+
* <code>1 <= s.length <= 10<sup>4</sup></code>
22+
* `s` 仅由小写英文字母组成
23+
* <code>1 <= k <= 10<sup>5</sup></code>
24+
25+
## 题解 (Rust)
26+
27+
### 1. 题解
28+
```Rust
29+
impl Solution {
30+
pub fn longest_substring(s: String, k: i32) -> i32 {
31+
Self::dfs(s.as_bytes(), 0, s.len() - 1, k as usize)
32+
}
33+
34+
fn dfs(s: &[u8], l: usize, r: usize, k: usize) -> i32 {
35+
if l > r || r - l + 1 < k {
36+
return 0;
37+
}
38+
39+
let mut indices = vec![vec![]; 26];
40+
let mut ret = (r - l + 1) as i32;
41+
42+
for i in l..=r {
43+
indices[(s[i] - b'a') as usize].push(i);
44+
}
45+
46+
for i in 0..26 {
47+
if !indices[i].is_empty() && indices[i].len() < k {
48+
ret = 0;
49+
ret = ret.max(Self::dfs(s, l, indices[i][0].saturating_sub(1), k));
50+
for j in 1..indices[i].len() {
51+
ret = ret.max(Self::dfs(s, indices[i][j - 1] + 1, indices[i][j] - 1, k));
52+
}
53+
ret = ret.max(Self::dfs(s, *indices[i].last().unwrap() + 1, r, k));
54+
break;
55+
}
56+
}
57+
58+
ret
59+
}
60+
}
61+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
impl Solution {
2+
pub fn longest_substring(s: String, k: i32) -> i32 {
3+
Self::dfs(s.as_bytes(), 0, s.len() - 1, k as usize)
4+
}
5+
6+
fn dfs(s: &[u8], l: usize, r: usize, k: usize) -> i32 {
7+
if l > r || r - l + 1 < k {
8+
return 0;
9+
}
10+
11+
let mut indices = vec![vec![]; 26];
12+
let mut ret = (r - l + 1) as i32;
13+
14+
for i in l..=r {
15+
indices[(s[i] - b'a') as usize].push(i);
16+
}
17+
18+
for i in 0..26 {
19+
if !indices[i].is_empty() && indices[i].len() < k {
20+
ret = 0;
21+
ret = ret.max(Self::dfs(s, l, indices[i][0].saturating_sub(1), k));
22+
for j in 1..indices[i].len() {
23+
ret = ret.max(Self::dfs(s, indices[i][j - 1] + 1, indices[i][j] - 1, k));
24+
}
25+
ret = ret.max(Self::dfs(s, *indices[i].last().unwrap() + 1, r, k));
26+
break;
27+
}
28+
}
29+
30+
ret
31+
}
32+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@
219219
[390][390l] |[Elimination Game][390] |![rs]
220220
[392][392l] |[Is Subsequence][392] |![rs]
221221
[393][393l] |[UTF-8 Validation][393] |![rs]
222+
[395][395l] |[Longest Substring with At Least K Repeating Characters][395] |![rs]
222223
[396][396l] |[Rotate Function][396] |![rs]
223224
[397][397l] |[Integer Replacement][397] |![rs]
224225
[398][398l] |[Random Pick Index][398] |![py]
@@ -1468,6 +1469,7 @@
14681469
[390]:Problemset/0390-Elimination%20Game/README.md#390-elimination-game
14691470
[392]:Problemset/0392-Is%20Subsequence/README.md#392-is-subsequence
14701471
[393]:Problemset/0393-UTF-8%20Validation/README.md#393-utf-8-validation
1472+
[395]:Problemset/0395-Longest%20Substring%20with%20At%20Least%20K%20Repeating%20Characters/README.md#395-longest-substring-with-at-least-k-repeating-characters
14711473
[396]:Problemset/0396-Rotate%20Function/README.md#396-rotate-function
14721474
[397]:Problemset/0397-Integer%20Replacement/README.md#397-integer-replacement
14731475
[398]:Problemset/0398-Random%20Pick%20Index/README.md#398-random-pick-index
@@ -2716,6 +2718,7 @@
27162718
[390l]:https://leetcode.com/problems/elimination-game/
27172719
[392l]:https://leetcode.com/problems/is-subsequence/
27182720
[393l]:https://leetcode.com/problems/utf-8-validation/
2721+
[395l]:https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/
27192722
[396l]:https://leetcode.com/problems/rotate-function/
27202723
[397l]:https://leetcode.com/problems/integer-replacement/
27212724
[398l]:https://leetcode.com/problems/random-pick-index/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@
219219
[390][390l] |[消除游戏][390] |![rs]
220220
[392][392l] |[判断子序列][392] |![rs]
221221
[393][393l] |[UTF-8 编码验证][393] |![rs]
222+
[395][395l] |[至少有 K 个重复字符的最长子串][395] |![rs]
222223
[396][396l] |[旋转函数][396] |![rs]
223224
[397][397l] |[整数替换][397] |![rs]
224225
[398][398l] |[随机数索引][398] |![py]
@@ -1468,6 +1469,7 @@
14681469
[390]:Problemset/0390-Elimination%20Game/README_CN.md#390-消除游戏
14691470
[392]:Problemset/0392-Is%20Subsequence/README_CN.md#392-判断子序列
14701471
[393]:Problemset/0393-UTF-8%20Validation/README_CN.md#393-utf-8-编码验证
1472+
[395]:Problemset/0395-Longest%20Substring%20with%20At%20Least%20K%20Repeating%20Characters/README_CN.md#395-至少有-k-个重复字符的最长子串
14711473
[396]:Problemset/0396-Rotate%20Function/README_CN.md#396-旋转函数
14721474
[397]:Problemset/0397-Integer%20Replacement/README_CN.md#397-整数替换
14731475
[398]:Problemset/0398-Random%20Pick%20Index/README_CN.md#398-随机数索引
@@ -2716,6 +2718,7 @@
27162718
[390l]:https://leetcode.cn/problems/elimination-game/
27172719
[392l]:https://leetcode.cn/problems/is-subsequence/
27182720
[393l]:https://leetcode.cn/problems/utf-8-validation/
2721+
[395l]:https://leetcode.cn/problems/longest-substring-with-at-least-k-repeating-characters/
27192722
[396l]:https://leetcode.cn/problems/rotate-function/
27202723
[397l]:https://leetcode.cn/problems/integer-replacement/
27212724
[398l]:https://leetcode.cn/problems/random-pick-index/

0 commit comments

Comments
 (0)