当前位置:网站首页>【日常训练】395. 至少有 K 个重复字符的最长子串
【日常训练】395. 至少有 K 个重复字符的最长子串
2022-07-03 15:05:00 【Puppet__】
题目
给你一个字符串 s 和一个整数 k ,请你找出 s 中的最长子串, 要求该子串中的每一字符出现次数都不少于 k 。返回这一子串的长度。
示例 1:
输入:s = “aaabb”, k = 3
输出:3
解释:最长子串为 “aaa” ,其中 ‘a’ 重复了 3 次。
示例 2:
输入:s = “ababbc”, k = 2
输出:5
解释:最长子串为 “ababb” ,其中 ‘a’ 重复了 2 次, ‘b’ 重复了 3 次。
提示:
1 <= s.length <= 104
s 仅由小写英文字母组成
1 <= k <= 105
代码
package dayLeetCode;
public class dayleetcode395 {
// 分治,通过出现次数大于0且小于k的字符将字符串划分成若干段
public int longestSubstring(String s, int k) {
int n = s.length();
return dfs(s, 0 , n - 1, k);
}
private int dfs(String s, int l, int r, int k) {
// 统计每个字符出现次数
int[] cnt = new int[26];
for (int i = l; i <= r; i++){
cnt[s.charAt(i) - 'a']++;
}
// 统计小于k的字符
char ch = 0;
for (int i = 0; i < 26; i++){
if (cnt[i] > 0 && cnt[i] < k){
ch = (char) (i + 'a');
break;
}
}
// 全部大于k
if (ch == 0){
return r - l + 1;
}
int t = l;
int ans = 0;
while (t <= r){
while (t <= r && s.charAt(t) == ch){
t++;
}
if (t > r){
break;
}
int start = t;
// 计算该段最右端的位置
while (t <= r && s.charAt(t) != ch){
t++;
}
// 分治
int len = dfs(s, start, t - 1, k);
ans = Math.max(ans, len);
}
return ans;
}
public static void main(String[] args) {
dayleetcode395 obj = new dayleetcode395();
System.out.println(obj.longestSubstring("aaabb", 3));
}
}
边栏推荐
- [engine development] rendering architecture and advanced graphics programming
- C string format (decimal point retention / decimal conversion, etc.)
- Nppexec get process return code
- App全局异常捕获
- There are links in the linked list. Can you walk three steps faster or slower
- Yolov5系列(一)——网络可视化工具netron
- SQL server安装位置改不了
- 【Transform】【实践】使用Pytorch的torch.nn.MultiheadAttention来实现self-attention
- C language memory function
- Global and Chinese markets for flexible chips 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
![[attention mechanism] [first vit] Detr, end to end object detection with transformers the main components of the network are CNN and transformer](/img/9b/6ca8375ef8689a80d437665909ae30.png)
[attention mechanism] [first vit] Detr, end to end object detection with transformers the main components of the network are CNN and transformer

运维体系的构建

Yolov5 series (I) -- network visualization tool netron

Besides lying flat, what else can a 27 year old do in life?

Functional modules and application scenarios covered by the productization of user portraits

cpu飙升排查方法

【微信小程序】WXSS 模板样式

Pytorch深度学习和目标检测实战笔记

Influxdb2 sources add data sources

Didi off the shelf! Data security is national security
随机推荐
Vs+qt multithreading implementation -- run and movetothread
Search in the two-dimensional array of leetcode sword offer (10)
【pytorch学习笔记】Datasets and Dataloaders
Global and Chinese market of solder bars 2022-2028: Research Report on technology, participants, trends, market size and share
基础SQL教程
NOI OPENJUDGE 1.3(06)
Container of symfony
cpu飙升排查方法
[combinatorics] permutation and combination (set permutation, step-by-step processing example)
使用JMeter对WebService进行压力测试
零拷贝底层剖析
Yolov5系列(一)——网络可视化工具netron
Finally, someone explained the financial risk management clearly
Introduction to opengl4.0 tutorial computing shaders
远程服务器后台挂起 nohup
C language fcntl function
[opengl] bone animation blending effect
How to color ordinary landscape photos, PS tutorial
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
PS tips - draw green earth with a brush