当前位置:网站首页>[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 market of marketing automation 2022-2028: Research Report on technology, participants, trends, market size and share
- CentOS7部署哨兵Redis(带架构图,清晰易懂)
- Relationship between truncated random distribution and original distribution
- Search in the two-dimensional array of leetcode sword offer (10)
- XWiki安装使用技巧
- 【日常训练】395. 至少有 K 个重复字符的最长子串
- Global and Chinese market of Bus HVAC systems 2022-2028: Research Report on technology, participants, trends, market size and share
- [transform] [NLP] first proposed transformer. The 2017 paper "attention is all you need" by Google brain team
- Halcon与Winform学习第二节
- 开启 Chrome 和 Edge 浏览器多线程下载
猜你喜欢

Kubernetes带你从头到尾捋一遍

Devaxpress: range selection control rangecontrol uses

第04章_逻辑架构

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

Dataframe returns the whole row according to the value
![[graphics] real shading in Unreal Engine 4](/img/8d/53775c7570c5578f4fe985592bb305.jpg)
[graphics] real shading in Unreal Engine 4

Solve the problem that pushgateway data will be overwritten by multiple push

Yolov5系列(一)——網絡可視化工具netron

5.4-5.5

CentOS7部署哨兵Redis(带架构图,清晰易懂)
随机推荐
Global and Chinese market of trimethylamine 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)
High quality workplace human beings must use software to recommend, and you certainly don't know the last one
The first character of leetcode sword offer that only appears once (12)
[transform] [NLP] first proposed transformer. The 2017 paper "attention is all you need" by Google brain team
Pytorch深度学习和目标检测实战笔记
The state does not change after the assignment of El switch
Byte practice surface longitude
Center and drag linked global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of solder bars 2022-2028: Research Report on technology, participants, trends, market size and share
Detailed comments on MapReduce instance code on the official website
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
4-20-4-23 concurrent server, TCP state transition;
C string format (decimal point retention / decimal conversion, etc.)
[set theory] inclusion exclusion principle (complex example)
SQL server安装位置改不了
Apache ant extension tutorial
Global and Chinese markets for ionization equipment 2022-2028: Research Report on technology, participants, trends, market size and share
Solve the problem that pushgateway data will be overwritten by multiple push
Global and Chinese market of lighting control components 2022-2028: Research Report on technology, participants, trends, market size and share