思路:

尺取法。

循环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. SSH框架中hibernate 出现 user is not mapped 问题

    SSH框架中hibernate 出现 user is not mapped 问题      在做SSH框架整合时,在进行DAO操作时.这里就只调用了chekUser()方法.运行时报  user is ...

  2. POJ1226(strstr)

    Substrings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13348   Accepted: 4722 Descr ...

  3. 修改winform安装包写日志文件权限

    1.如果程序有写入文件的功能要添加该文件 并配置该属性 改成这个即可

  4. webpack 知识点总结

    1 webpack 的不同点: 1.1 实现代码分割 1.2 loaders 1.3 plugin system 2 webpack 打包css,需要使用css-loaders 使webpack识别c ...

  5. docker 学习(五) virtualBox虚拟机安装docker

    这里计划用virtualBox虚拟机安装两个ubuntu servers, 然后用docker把spring boot项目部署上去,模拟一下分布式的微服务情况. 1:安装virtualbox,后安装U ...

  6. CS231n 2016 通关 第四章-反向传播与神经网络(第一部分)

    在上次的分享中,介绍了模型建立与使用梯度下降法优化参数.梯度校验,以及一些超参数的经验. 本节课的主要内容: 1==链式法则 2==深度学习框架中链式法则 3==全连接神经网络 =========== ...

  7. Windows服务卸载服务窗口仍显示的问题

    关于Windows服务通过命令卸载后, 打开服务窗口,服务还有显示,只是状态改为了禁用,运行停止.那么我们怎么解决呢, 不要心慌,打开你的任务管理器,查看服务所用的exe程序是否还在运行,若有的话,便 ...

  8. office2016专业增强版安装包和激活工具

    链接:https://pan.baidu.com/s/1j_gvpNBWo1rQ0xB49I_Ttw 密码:v2w7

  9. Uploadify API在项目上的应用

    在项目开发中,前端使用easyui,jq的时候,我么涉及到导入的时候都要用到这个上传插件,用法是: 1:先初始化上传控件 2:打开导入的easyui dialog弹出框,dialog里面将上传的inp ...

  10. Flutter实战视频-移动电商-64.会员中心_顶部头像UI布局

    64.会员中心_顶部头像UI布局 会员中心的样式 member.dart 清除原来的代码生成一个基本的结构 默认返回一个scaffold脚手架工具,body里面布局使用ListView,这样不会出现纵 ...