当前位置:网站首页>【日常训练】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));
}
}
边栏推荐
- 5.2-5.3
- [ue4] cascading shadow CSM
- [opengl] advanced chapter of texture - principle of flowmap
- Yolov5进阶之九 目标追踪实例1
- C language to implement a password manager (under update)
- Using TCL (tool command language) to manage Tornado (for VxWorks) can start the project
- 官网MapReduce实例代码详细批注
- 复合类型(自定义类型)
- Yolov5 series (I) -- network visualization tool netron
- TPS61170QDRVRQ1
猜你喜欢
The picture quality has been improved! LR enhancement details_ Lightroom turns on AI photo detail enhancement: picture clarity increases by 30%
ASTC texture compression (adaptive scalable texture compression)
【Transformer】入门篇-哈佛Harvard NLP的原作者在2018年初以逐行实现的形式呈现了论文The Annotated Transformer
【注意力机制】【首篇ViT】DETR,End-to-End Object Detection with Transformers网络的主要组成是CNN和Transformer
[set theory] inclusion exclusion principle (complex example)
There are links in the linked list. Can you walk three steps faster or slower
Incluxdb2 buckets create database
Série yolov5 (i) - - netron, un outil de visualisation de réseau
High quality workplace human beings must use software to recommend, and you certainly don't know the last one
Introduction to opengl4.0 tutorial computing shaders
随机推荐
Centos7 deployment sentry redis (with architecture diagram, clear and easy to understand)
C language DUP function
Global and Chinese markets for transparent OLED displays 2022-2028: Research Report on technology, participants, trends, market size and share
Piwigo 2.7.1 sqli learning
On MEM series functions of C language
零拷贝底层剖析
Finally, someone explained the financial risk management clearly
4-29——4.32
"Seven weapons" in the "treasure chest" of machine learning: Zhou Zhihua leads the publication of the new book "machine learning theory guide"
Leetcode sword offer find the number I (nine) in the sorted array
【Transform】【实践】使用Pytorch的torch.nn.MultiheadAttention来实现self-attention
【pytorch学习笔记】Transforms
Global and Chinese market of Bus HVAC systems 2022-2028: Research Report on technology, participants, trends, market size and share
4-33--4-35
Explanation of time complexity and space complexity
How does vs+qt set the software version copyright, obtain the software version and display the version number?
.NET六大设计原则个人白话理解,有误请大神指正
Functional modules and application scenarios covered by the productization of user portraits
What is label encoding? How to distinguish and use one hot encoding and label encoding?
QT - draw something else