题意

给定一个字符串,求得子字符串的最长长度,使得每一个字符最小重复K次

题解

可以先统计出每个字符的次数,然后遍历字符串,对于字符次数小于K的就跳过,重新设置起始点,对于字符次数大于K的end+1,end初始值为start+1;

class Solution {
public:int res=0;int longestSubstring(string s, int k) {if(k>s.length()){//even if all character are same they can't have freq k//as k>length of arrayreturn 0;}return helper(s,0,s.length(),k);}int helper(string s,int start,int end,int k){if(end-start<k)return  0;//we get the freq of the every charcter in s between start and endvector<int> freq(26,0);for(int i=start;i<end;i++){freq[s[i]-'a']++;   }//get the substring again where freq of every character is greater than kfor(int i=start;i<end;i++){if(freq[s[i]-'a']<k){//this cannot form result//now find the last index upto which we cannot make resultint j=i+1;while(j<end && freq[s[j]-'a']<k)j++;//cal the max substr from left and rightreturn max(helper(s,start,i,k),helper(s,j,end,k));}}//if all character freq >=k return end-start;}
};
class Solution {
public:int longestSubstring(string s, int k) {int n = s.length();// if length of string is 0 or it is less than k then there will no longest substring so we will return 0.if(n == 0 or n < k) return 0;//k ==1 means that all the characters will be unique so we will return entire length.if(k <= 1) return n;//count map to store count of charactersunordered_map<char,int> countMap;for(char c : s) countMap[c]++;int left=0;while(left<n && countMap[s[left]] >=k) left++;if(left > n-1) return left;int l1 = longestSubstring(s.substr(0, left), k);while(left < n && countMap[s[left]]<k) left++;// to check for longest Substring in part after leftint l2 = left < n ? longestSubstring(s.substr(left),k) : 0;//return max of l1 and l2return max(l1,l2);}
};

395. Longest Substring with At Least K Repeating Characters 1相关推荐

  1. 395. Longest Substring with At Least K Repeating Characters

    题目要求 Find the length of the longest substring T of a given string (consists of lowercase letters onl ...

  2. leetcode 395. Longest Substring with At Least K Repeating Characters| 395. 至少有 K 个重复字符的最长子串(分治法)

    题目 https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/ 题解 参考:官方题解 ...

  3. LeetCode Longest Substring with At Least K Repeating Characters(递归)

    问题:给出一个字符串s,要求求出最长的子串,每个字符出现至少k次 思路: 方法一,先统计每个字符出现的次数,然后分别从头和尾找出出现资料最小为k的位置,然后再此区间遍历,当出现小于k次时,在两个子区间 ...

  4. LeetCode 340. Longest Substring with At Most K Distinct Characters

    原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...

  5. [LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串...

    Given a string, find the length of the longest substring T that contains at most k distinct characte ...

  6. Longest Substring With At Most K Distinct Characters

    Given a string, find the length of the longest substring T that contains at most k distinct characte ...

  7. 386 · Longest Substring with At Most K Distinct Characters最多有k个不同字符的最长子字符串

    链接:https://www.lintcode.com/problem/386/description https://mp.weixin.qq.com/s?__biz=MzU2OTUyNzk1NQ= ...

  8. [LeetCode] 159. Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串...

    Given a string S, find the length of the longest substring T that contains at most two distinct char ...

  9. LeetCode 159. Longest Substring with At Most Two Distinct Characters --Java,C++,Python解法

    题目地址:Longest Substring with At Most Two Distinct Characters - LeetCode Given a string s , find the l ...

  10. [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串...

    Given a string S, find the length of the longest substring T that contains at most two distinct char ...

最新文章

  1. ELK教程1:ElasticSearch集群的部署
  2. springboot webjar使用
  3. FPGA的设计艺术(18)如何使用Verilog中的数组对存储器进行建模?
  4. idea、eclipse常用快捷键
  5. linux文件操作命令--转
  6. 【Linux】一步一步学Linux——ntsysv命令(149)
  7. rx 异步执行耗时_使用rx-java的异步抽象
  8. 笨方法python_笨方法学习Python(11-20)
  9. 剑指offer——32.从上到下打印二叉树
  10. 程序员面试金典——5.3最接近的数
  11. CorelDRAWX4的VBA插件开发(六)录制宏与调试
  12. ubuntu防火墙安装arm架构说明
  13. 使用优盘为龙芯电脑安装系统
  14. java实训心得感想30字,java实训心得体会感想
  15. pdf文件太大怎么压缩?四个步骤完成
  16. windows10系统更新后,Windows.old 无法删除解决方法
  17. 物联网的通信技术以及Wi-Fi、一键配网技术、BLE、GPRS(2G)、LTE-Cat1 、NB-IoT简介
  18. 低代码平台开发 python_【低筋面粉】低筋面粉的功效_低筋面粉图片_食材百科_美食杰...
  19. 吉大计算机专业的硕士论文,吉林大学硕士研究生学位论文陷入抄袭之门
  20. Java有没有lower_bound函数_函数 - lower_boundupper_bound

热门文章

  1. 如何解决安装或者卸载时 临时文件夹已满或不能访问
  2. ERPLAB脑电数据分析教程
  3. asp.net/c# 注册页实现激活邮箱验证
  4. CHM 已取消到该网页的导航
  5. 2018-11-15-mqtt-mosquitto系列11之配置基于ca证书的桥接
  6. 360校招笔试算法题
  7. centos7 mysql启动后端口,centos安装mysql后3306端口不通_网站服务器运行维护
  8. 深度学习论文: Pyramidal Convolution: Rethinking CNN for Visual Recognition及其PyTorch实现
  9. 解决IntelliJ IDEA Properties中Unused property提示
  10. Win11打不开本地组策略编辑器怎么办