当前位置:网站首页>[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));
}
}
边栏推荐
- Basic SQL tutorial
- 【注意力机制】【首篇ViT】DETR,End-to-End Object Detection with Transformers网络的主要组成是CNN和Transformer
- Vs+qt multithreading implementation -- run and movetothread
- The state does not change after the assignment of El switch
- [attention mechanism] [first vit] Detr, end to end object detection with transformers the main components of the network are CNN and transformer
- [ue4] Niagara's indirect draw
- 视觉上位系统设计开发(halcon-winform)-1.流程节点设计
- 【Transform】【实践】使用Pytorch的torch.nn.MultiheadAttention来实现self-attention
- .NET六大设计原则个人白话理解,有误请大神指正
- [graphics] real shading in Unreal Engine 4
猜你喜欢

复合类型(自定义类型)

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

Byte practice plane longitude 2

B2020 分糖果

Basic SQL tutorial
![[probably the most complete in Chinese] pushgateway entry notes](/img/5a/6dcb75f5d713ff513ad6842ff53cc3.png)
[probably the most complete in Chinese] pushgateway entry notes

Kubernetes vous emmène du début à la fin
![[graphics] hair simulation in tressfx](/img/41/cef55811463d3a25a29ddab5278af0.jpg)
[graphics] hair simulation in tressfx

【Transformer】入门篇-哈佛Harvard NLP的原作者在2018年初以逐行实现的形式呈现了论文The Annotated Transformer

CentOS7部署哨兵Redis(带架构图,清晰易懂)
随机推荐
Global and Chinese market of iron free motors 2022-2028: Research Report on technology, participants, trends, market size and share
Web server code parsing - thread pool
5.2-5.3
使用Tengine解决负载均衡的Session问题
Global and Chinese market of solder bars 2022-2028: Research Report on technology, participants, trends, market size and share
Redis主从、哨兵、集群模式介绍
Incluxdb2 buckets create database
B2020 points candy
Dataframe returns the whole row according to the value
Vs+qt application development, set software icon icon
Vs+qt multithreading implementation -- run and movetothread
Yolov5 advanced seven target tracking latest environment construction (II)
C # realizes the login interface, and the password asterisk is displayed (hide the input password)
The first character of leetcode sword offer that only appears once (12)
视觉上位系统设计开发(halcon-winform)-4.通信管理
[engine development] in depth GPU and rendering optimization (basic)
Halcon与Winform学习第二节
redis单线程问题强制梳理门外汉扫盲
Global and Chinese market of trimethylamine 2022-2028: Research Report on technology, participants, trends, market size and share
Open under vs2019 UI file QT designer flash back problem