当前位置:网站首页>[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));
}
}
边栏推荐
- 视觉上位系统设计开发(halcon-winform)-5.相机
- 4-29——4.32
- Relationship between truncated random distribution and original distribution
- Web server code parsing - thread pool
- 第04章_逻辑架构
- Yolov5 advanced seven target tracking latest environment construction (II)
- TPS61170QDRVRQ1
- Besides lying flat, what else can a 27 year old do in life?
- Tensor ellipsis (three points) slice
- XWiki安装使用技巧
猜你喜欢
Yolov5系列(一)——網絡可視化工具netron
【Transform】【NLP】首次提出Transformer,Google Brain团队2017年论文《Attention is all you need》
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
Functional modules and application scenarios covered by the productization of user portraits
Troubleshooting method of CPU surge
[ue4] Niagara's indirect draw
C # realizes the login interface, and the password asterisk is displayed (hide the input password)
Pytorch深度学习和目标检测实战笔记
How does vs+qt set the software version copyright, obtain the software version and display the version number?
[pytorch learning notes] datasets and dataloaders
随机推荐
什么是Label encoding?one-hot encoding ,label encoding两种编码该如何区分和使用?
Basic SQL tutorial
[attention mechanism] [first vit] Detr, end to end object detection with transformers the main components of the network are CNN and transformer
[opengl] advanced chapter of texture - principle of flowmap
4-24--4-28
Construction of operation and maintenance system
链表有环,快慢指针走3步可以吗
【Transform】【实践】使用Pytorch的torch.nn.MultiheadAttention来实现self-attention
Kubernetes - YAML文件解读
[transformer] Introduction - the original author of Harvard NLP presented the annotated transformer in the form of line by line implementation in early 2018
Global and Chinese market of postal automation systems 2022-2028: Research Report on technology, participants, trends, market size and share
[ue4] Niagara's indirect draw
el-switch 赋值后状态不变化
App全局异常捕获
【pytorch学习笔记】Datasets and Dataloaders
Detailed comments on MapReduce instance code on the official website
Global and Chinese markets for infrared solutions (for industrial, civil, national defense and security applications) 2022-2028: Research Report on technology, participants, trends, market size and sh
There are links in the linked list. Can you walk three steps faster or slower
406. Reconstruct the queue according to height
[set theory] inclusion exclusion principle (complex example)