当前位置:网站首页>[daily training] 395 Longest substring with at least k repeated characters
[daily training] 395 Longest substring with at least k repeated characters
2022-07-03 15:11:00 【Puppet__】
subject
Give you a string s And an integer k , Please find out s Longest substring in , The number of occurrences of each character in the substring shall not be less than k . Returns the length of this substring .
Example 1:
Input :s = “aaabb”, k = 3
Output :3
explain : The longest string is “aaa” , among ‘a’ repeated 3 Time .
Example 2:
Input :s = “ababbc”, k = 2
Output :5
explain : The longest string is “ababb” , among ‘a’ repeated 2 Time , ‘b’ repeated 3 Time .
Tips :
1 <= s.length <= 104
s It's only made up of lowercase letters
1 <= k <= 105
Code
package dayLeetCode;
public class dayleetcode395 {
// Divide and conquer , Pass occurs more than 0 And less than k The character of divides the string into segments
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) {
// Count the number of occurrences of each character
int[] cnt = new int[26];
for (int i = l; i <= r; i++){
cnt[s.charAt(i) - 'a']++;
}
// Statistics less than k The characters of
char ch = 0;
for (int i = 0; i < 26; i++){
if (cnt[i] > 0 && cnt[i] < k){
ch = (char) (i + 'a');
break;
}
}
// All greater than 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;
// Calculate the position of the rightmost end of the segment
while (t <= r && s.charAt(t) != ch){
t++;
}
// Divide and conquer
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));
}
}
边栏推荐
- [set theory] inclusion exclusion principle (complex example)
- What are the composite types of Blackhorse Clickhouse, an OLAP database recognized in the industry
- Global and Chinese market of optical fiber connectors 2022-2028: Research Report on technology, participants, trends, market size and share
- 解决pushgateway数据多次推送会覆盖的问题
- mysql innodb 存储引擎的特性—行锁剖析
- 5-1 blocking / non blocking, synchronous / asynchronous
- Vs+qt multithreading implementation -- run and movetothread
- High quality workplace human beings must use software to recommend, and you certainly don't know the last one
- 什么是Label encoding?one-hot encoding ,label encoding两种编码该如何区分和使用?
- 零拷贝底层剖析
猜你喜欢
MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)
Pytorch深度学习和目标检测实战笔记
Kubernetes 进阶训练营 Pod基础
C string format (decimal point retention / decimal conversion, etc.)
链表有环,快慢指针走3步可以吗
[transformer] Introduction - the original author of Harvard NLP presented the annotated transformer in the form of line by line implementation in early 2018
High quality workplace human beings must use software to recommend, and you certainly don't know the last one
解决pushgateway数据多次推送会覆盖的问题
Halcon与Winform学习第二节
mysql innodb 存储引擎的特性—行锁剖析
随机推荐
Remote server background hangs nohup
Yolov5系列(一)——网络可视化工具netron
5.4-5.5
Zero copy underlying analysis
北京共有产权房出租新规实施的租赁案例
官网MapReduce实例代码详细批注
Mmdetection learning rate and batch_ Size relationship
【可能是全中文网最全】pushgateway入门笔记
Kubernetes - YAML文件解读
The method of parameter estimation of user-defined function in MATLAB
PyTorch crop images differentiablly
Kubernetes帶你從頭到尾捋一遍
2022/02/14
mysql innodb 存储引擎的特性—行锁剖析
【云原生训练营】模块七 Kubernetes 控制平面组件:调度器与控制器
【Transformer】入门篇-哈佛Harvard NLP的原作者在2018年初以逐行实现的形式呈现了论文The Annotated Transformer
XWiki安装使用技巧
Global and Chinese markets for transparent OLED displays 2022-2028: Research Report on technology, participants, trends, market size and share
Yolov5 advanced seven target tracking latest environment construction (II)
【日常训练】395. 至少有 K 个重复字符的最长子串