当前位置:网站首页>[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));
}
}
边栏推荐
- 使用Tengine解决负载均衡的Session问题
- 4-33--4-35
- MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)
- [combinatorics] permutation and combination (set permutation, step-by-step processing example)
- PyTorch crop images differentiablly
- 【注意力机制】【首篇ViT】DETR,End-to-End Object Detection with Transformers网络的主要组成是CNN和Transformer
- Search in the two-dimensional array of leetcode sword offer (10)
- [ue4] material and shader permutation
- 【日常训练】395. 至少有 K 个重复字符的最长子串
- Vs+qt multithreading implementation -- run and movetothread
猜你喜欢
[transform] [NLP] first proposed transformer. The 2017 paper "attention is all you need" by Google brain team
[set theory] inclusion exclusion principle (complex example)
什么是embedding(把物体编码为一个低维稠密向量),pytorch中nn.Embedding原理及使用
【pytorch学习笔记】Datasets and Dataloaders
How does vs+qt set the software version copyright, obtain the software version and display the version number?
Redis主从、哨兵、集群模式介绍
Vs+qt application development, set software icon icon
解决pushgateway数据多次推送会覆盖的问题
[graphics] real shading in Unreal Engine 4
QT program font becomes larger on computers with different resolutions, overflowing controls
随机推荐
Global and Chinese markets for sterile packaging 2022-2028: Research Report on technology, participants, trends, market size and share
Zero copy underlying analysis
2022/02/14
Class part2
[transform] [NLP] first proposed transformer. The 2017 paper "attention is all you need" by Google brain team
Global and Chinese markets of AC electromechanical relays 2022-2028: Research Report on technology, participants, trends, market size and share
Neon global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
5.4-5.5
高并发下之redis锁优化实战
[ue4] material and shader permutation
Mysql报错:[ERROR] mysqld: File ‘./mysql-bin.010228‘ not found (Errcode: 2 “No such file or directory“)
App global exception capture
链表有环,快慢指针走3步可以吗
[engine development] rendering architecture and advanced graphics programming
QT program font becomes larger on computers with different resolutions, overflowing controls
.NET六大设计原则个人白话理解,有误请大神指正
How does vs+qt set the software version copyright, obtain the software version and display the version number?
Functional modules and application scenarios covered by the productization of user portraits
Yolov5 advanced nine target tracking example 1
Open under vs2019 UI file QT designer flash back problem