题目链接

对一个字符串,找出一个最长的子串长度,这个子串中所有字符出现的次数至少为k。

1.滑动窗口

一开始把题目看成了,子串中每个字符至多出现k次。如果是这样,那么是一道典型的滑动窗口的题目。

然而,题目是至少出现k次。这样一来,滑动窗口不再适用。因为,在字符出现至多k次的问题中,当窗口尾部的字符超过k个,意味着只需要将窗口头部往后移动直至尾部字符等于k个;当尾部字符出现次数小于k次时,只需要将窗口尾部继续往后移动即可。而在字符出现至少k次的问题中,当尾部字符出现次数小于k次时,无法判断是移动窗口头部(舍弃尾部当前字符)还是移动窗口尾部(期待后面还有更多当前尾部字符)。

2.前缀和

然后想到了前缀和计数,然后用n^2的时间复杂度,检查每个子串是否满足所有字符至少出现k次。不出所料的超时。

3.一个想法

先对整个字符串的字符进行计数,假如存在出现次数少于k次的字符,那么目标串一定出现在被这个字符分隔开的几个字符串中。想到这里,我并没有得到什么更多的思路。。。

4.看了眼题解后

往深一步想,假如存在出现次数少于k次的字符,那么目标串一定出现在被这个字符分隔开的几个字符串中。那么,对这里的每一个分割产生的字符串,再求所有字符出现次数大于k的最长子串。不就得到原问题的一个子问题了吗?也就找到了递归关系。

class Solution {
public:
vector<string> split(string s, char c){
string temp;
vector<string> ret;
for(decltype(s.size())i=; i<s.size(); i++){
if(s[i] == c){
if(temp.size()){
ret.push_back(temp);
temp = "";
}
}else{
temp += s[i];
}
}
if(temp.size())
ret.push_back(temp);
return ret;
} int longestSubstring(string s, int k) {
vector<int> ctr(, );
for(auto c : s)
ctr[c-'a']++;
int ret = , i;
for(i=; i<; i++)
if(ctr[i] > && ctr[i] < k){
auto substrs = split(s, i+'a');
for(auto str : substrs)
ret = max(ret, longestSubstring(str, k));
break;
}
if(i == )
return s.size();
return ret;
}
};

Leetcode_395. Longest Substring with At Least K Repeating Characters_[Devide and Conquer]的更多相关文章

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

    395. Longest Substring with At Least K Repeating Characters 我的思路是先扫描一遍,然后判断是否都满足,否则,不满足的字符一定不出现,可以作为 ...

  2. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  3. [LeetCode] 395. Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. [Swift]LeetCode395. 至少有K个重复字符的最长子串 | Longest Substring with At Least K Repeating Characters

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  5. 2016/9/21 leetcode 解题笔记 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 only) su ...

  6. Leetcode: Longest Substring with At Least K Repeating Characters

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  7. LeetCode 395. Longest Substring with At Least K Repeating Characters C#

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  8. leetcode 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 only) su ...

  9. 【LeetCode】395. Longest Substring with At Least K Repeating Characters 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...

随机推荐

  1. 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第5节 String类_9_练习:按指定格式拼接字符

    数组再加一个值

  2. 06 使用bbed修复delete的数据--01

    06 使用bbed修复delete的数据--01 根据rowid查看数据文件和block号 SYS@ orcl ; ROWID ID NAME FILE# BLOCK# --------------- ...

  3. ceph部署-集群建立

    一.配置storage集群1.建立集群管理目录(管理配置文件,密钥)mkdir ceph-clustercd ceph-cluster/ 2.创建一个新集群(需要先将主机名加入/etc/hosts 必 ...

  4. lesson1-图的概念和图论模型

    说明: 图论专题开设的目的主要是作为本学期复习巩固和分享自己对于图论的理解,主要参考的是老师的PPT.应老师要求,不能共享文件,抱歉! 参考书目:[1] J.A. Bondy,  U.S.R. Mur ...

  5. [Web 前端] 013 css 内外边距

    1. css 内间距 也称:"内补白"或"内补丁" 参数 释义 padding 检索或设置对象四边的内部边距,如 padding:10px; padding:5 ...

  6. mysql 小数位

    1    select convert(t/100,decimal(15,2)) as a from user (1) convert() 一.在mysql操作中我们经常需要对数据进行类型转换.此时我 ...

  7. EasyUI之DataGrid分页

    第一步创建分页DataGrid <table id="dg"> <thead> <tr> <th data-options="f ...

  8. Version Controlling with Git in Visual Studio Code and Azure DevOps

    Overview Azure DevOps supports two types of version control, Git and Team Foundation Version Control ...

  9. 20191202IIS

    IIS和.netfw4.0安装顺序是从前到后,如果不小心颠倒了,无所谓. 打开程序-运行-cmd:输入一下命令重新注册IIS C:\WINDOWS\Microsoft.NET\Framework\v4 ...

  10. srs-librtmp pusher(push h264 raw)

    Simple Live System Using SRS https://www.cnblogs.com/dong1/p/5100792.html 1.上面是推送文件,改成推送缓存 封装了三个函数 i ...