思路:

尺取法。

循环i:1~26,分别计算恰好包含i种字母并且每种字母出现的次数大于等于k个的最长子串长度。

没法直接使用尺取法,因为不满足区间单调性,但是使用如上的方法却是可以的,因为子串中包含的字母种类数是满足区间单调性的。

实现:

 #include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int longestSubstring(string s, int k)
{
int n = s.length();
if (k == ) return n;
int ans = ;
vector<int> num(, );
for (int i = ; i <= ; i++)
{
fill(num.begin(), num.end(), );
int slow = , fast = , cnt = ;
set<char> st;
while (fast < n)
{
while (fast < n)
{
if (num[s[fast] - 'a'] == ) cnt++;
num[s[fast] - 'a']++;
st.insert(s[fast]);
fast++;
if (cnt == i && fast < n && num[s[fast] - 'a'] == )
break;
}
bool flg = true;
for (auto it: st)
if (num[it - 'a'] < k) { flg = false; break; }
if (flg) ans = max(ans, fast - slow);
if (fast == n) break;
while (slow < fast && cnt == i)
{
num[s[slow] - 'a']--;
if (num[s[slow] - 'a'] == ) { cnt--; st.erase(s[slow]); }
slow++;
}
}
}
return ans;
}
};
int main()
{
string s = "aabbccdcccde"; int k = ;
cout << Solution().longestSubstring(s, k) << endl;
return ;
}

leetcode395 Longest Substring with At Least K Repeating Characters的更多相关文章

  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. C++之log4cpp库的使用

    log4..简介 log4..是基于log4j的一系列的c++移植版本,使用了log4j的模式结构,目前主要有以下几个版本: 1. log4cxx, 目前是到0.10.0版,Apache下的孵化项目, ...

  2. 洛谷P4013数字梯形问题——网络流24题

    题目:https://www.luogu.org/problemnew/show/P4013 最大费用最大流裸题: 注意:在第二种情况中,底层所有点连向汇点的边容量应该为inf,因为可以有多条路径结束 ...

  3. 896C

    ODT/珂朵莉树 原来这个东西很咸鱼,只能数据随机情况下nloglogn,不过作为卡常还是很好的 大概就是维护区间,值相同的并且连续当成一个区间存在set里,每次区间操作强行分裂就行了. 复杂度因为是 ...

  4. [hdu2457]DNA repair(AC自动机+dp)

    题意:给出一些不合法的模式DNA串,给出一个原串,问最少需要修改多少个字符,使得原串中不包含非法串. 解题关键:多模式串匹配->AC自动机,求最优值->dp,注意在AC自动机上dp的套路. ...

  5. 20个Flutter实例视频教程-第05节: 酷炫的路由动画-1

    视屏地址: https://www.bilibili.com/video/av39709290/?p=5 博客地址: https://jspang.com/post/flutterDemo.html# ...

  6. XMLHttpRequest的用法

    转: 传统的Web应用请求服务器返回的一般是是完整的HTML页面,这样往往就需要页面进行刷新操作,不仅耗时而且用户体验度也不好.最典型的代表就是form表单登录操作了.如果登录失败往往是跳转到原网页重 ...

  7. 如何在html中引入jsx文件

    不使用webpack工具做react项目 1.引入react相关js文件 <script src="https://cdn.staticfile.org/react/16.4.0/um ...

  8. HDU3038【种类并查集】

    题意: 给出m组区间[a,b],以及其区间的和,问有矛盾的有几组: 思路: 种类并查集. 主要是几个关系:同类元素的关系,父亲与儿子的关系,不同类元素的关系: 我们可以类似看作一个前缀和,sum[x] ...

  9. lightoj 1099【dijkstra/BFS】

    题意: 求 1-N 的第二长路,一条路可以重复走 if two or more shortest paths exist, the second-shortest path is the one wh ...

  10. unity3d 在UGUI中制作自适应调整大小的滚动布局控件

    http://blog.csdn.net/rcfalcon/article/details/43459387 在游戏中,我们很多地方需要用到scroll content的概念:我们需要一个容器,能够指 ...