当前位置:网站首页>【日常训练】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));
}
}
边栏推荐
- Search in the two-dimensional array of leetcode sword offer (10)
- QT - draw something else
- Write a 2-minute countdown.
- App全局异常捕获
- 5.2-5.3
- [wechat applet] wxss template style
- C # realizes the login interface, and the password asterisk is displayed (hide the input password)
- 解决pushgateway数据多次推送会覆盖的问题
- 使用JMeter对WebService进行压力测试
- Nppexec get process return code
猜你喜欢

【Transform】【NLP】首次提出Transformer,Google Brain团队2017年论文《Attention is all you need》

Tencent internship interview sorting

B2020 分糖果

Qt—绘制其他东西

C language to realize mine sweeping
![[transform] [practice] use pytoch's torch nn. Multiheadattention to realize self attention](/img/94/a9c7010fe9f14454469609ac4dd871.png)
[transform] [practice] use pytoch's torch nn. Multiheadattention to realize self attention

第04章_逻辑架构

【注意力机制】【首篇ViT】DETR,End-to-End Object Detection with Transformers网络的主要组成是CNN和Transformer

QT - draw something else

el-switch 赋值后状态不变化
随机推荐
[opengl] geometry shader
Global and Chinese market of iron free motors 2022-2028: Research Report on technology, participants, trends, market size and share
What is one hot encoding? In pytoch, there are two ways to turn label into one hot coding
Global and Chinese markets for transparent OLED displays 2022-2028: Research Report on technology, participants, trends, market size and share
NOI OPENJUDGE 1.3(06)
Container of symfony
4-29——4.32
B2020 points candy
App全局异常捕获
Global and Chinese markets for ionization equipment 2022-2028: Research Report on technology, participants, trends, market size and share
Center and drag linked global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
App global exception capture
Yolov5 advanced 8 format conversion between high and low versions
C language STR function
There are links in the linked list. Can you walk three steps faster or slower
使用JMeter对WebService进行压力测试
从书本《皮囊》摘录的几个句子
How to color ordinary landscape photos, PS tutorial
解决pushgateway数据多次推送会覆盖的问题
Leetcode sword offer find the number I (nine) in the sorted array