当前位置:网站首页>[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));
}
}
边栏推荐
- Global and Chinese markets for sterile packaging 2022-2028: Research Report on technology, participants, trends, market size and share
- Kubernetes带你从头到尾捋一遍
- B2020 分糖果
- [wechat applet] wxss template style
- 链表有环,快慢指针走3步可以吗
- 【pytorch学习笔记】Transforms
- Global and Chinese market of lighting control components 2022-2028: Research Report on technology, participants, trends, market size and share
- C # realizes the login interface, and the password asterisk is displayed (hide the input password)
- .NET六大设计原则个人白话理解,有误请大神指正
- cpu飙升排查方法
猜你喜欢
随机推荐
MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)
Vs+qt application development, set software icon icon
Use of Tex editor
Web server code parsing - thread pool
Functional modules and application scenarios covered by the productization of user portraits
什么是Label encoding?one-hot encoding ,label encoding两种编码该如何区分和使用?
4-24--4-28
C string format (decimal point retention / decimal conversion, etc.)
Global and Chinese market of postal automation systems 2022-2028: Research Report on technology, participants, trends, market size and share
视觉上位系统设计开发(halcon-winform)-5.相机
Apache ant extension tutorial
[opengl] advanced chapter of texture - principle of flowmap
Global and Chinese market of iron free motors 2022-2028: Research Report on technology, participants, trends, market size and share
CentOS7部署哨兵Redis(带架构图,清晰易懂)
Global and Chinese market of trimethylamine 2022-2028: Research Report on technology, participants, trends, market size and share
Basic SQL tutorial
. Net six design principles personal vernacular understanding, please correct if there is any error
Redis主从、哨兵、集群模式介绍
[ue4] material and shader permutation
Besides lying flat, what else can a 27 year old do in life?



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





