思路:

尺取法。

循环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. POJ1904(有向图缩点+输入输出挂参考)

    King's Quest Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 8311   Accepted: 3017 Cas ...

  2. 错误 128 无法将类型“string”隐式转换为“System.Windows.Forms.DataGridViewTextBoxColumn”

    原因是DataGridView中列的Name属性值和DataPropertyName属性值一样,比如Name="CardID",DataPropertyName="Car ...

  3. hibernate的优缺点和适用场合

    Hibernate优点: 1.对象化.人员以面相对象的思想来操作数据库.Hibernate支持许多面向对象的特性,如组合,继承,多态等. 2.更好的移植性.对于不同的数据库,开发者只需要使用相同的数据 ...

  4. 开始我的技术bolg之旅

    虽然做了快十年的IT但是真心觉得自己的水平很烂,IT这个行业keep learning很重要,之前每接触新东西都是随便学一下,有问题解决了就完事了,或者是再有问题临时抱佛脚,从来都没把学过或者做个的事 ...

  5. 最新sublimetext3080注册

    ----- BEGIN LICENSE -----K-20Single User LicenseEA7E-9401293A099EC1 C0B5C7C5 33EBF0CF BE82FE3BEAC216 ...

  6. 1.1- 1.2 hive入门

    一.hive是什么 由Facebook开源用于解决海量结构化日志的数据统计: Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射成一张表, 并提供类SQL查询功能: 构建在Had ...

  7. JAVA企业级开发-session,cookie(13)

    一. jsp jsp: java server pager        .java动态生成网页的一门技术 使用servlet生成网页时候遇到的问题,为什么使用jsp? 在servlet里面书写htm ...

  8. 如何解决启动Error:com.intellij.util.indexing.StorageException问题?

    启动tomcat时idea出现如下错误: Error:com.intellij.util.indexing.StorageException: com.intellij.util.indexing.S ...

  9. 01 | VIM基础攻略

    启动 vim 后,vim 处于 normal 模式. Step One: "i" -> insert 模式, ESC -> normal 模式: "x&quo ...

  10. 嵌入式C语言模块编写

    C 语言源文件 *.c 理想的模块化应该可以看成是一个黑盒子.即我们只关心模块提供的功能,而不管模块内部的实现细节.在大规模程序开发中,一个程序由很多个模块组成,这些模块的编写任务被分配到不同的人,编 ...