leetcode395 Longest Substring with At Least K Repeating Characters
思路:
尺取法。
循环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的更多相关文章
- 395. Longest Substring with At Least K Repeating Characters
395. Longest Substring with At Least K Repeating Characters 我的思路是先扫描一遍,然后判断是否都满足,否则,不满足的字符一定不出现,可以作为 ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【LeetCode】395. Longest Substring with At Least K Repeating Characters 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...
随机推荐
- distutils 打包setup.py
from distutils.core import setup setup(name='hello', version='1.0', description='test example', auth ...
- selenium_page_object
最简单的pageobject github地址:https://github.com/defnngj/selenium_page_objects
- 【216】◀▶ IDL 字符串操作说明 (黑底)
参考:String Processing Routines —— 字符串处理函数 参考:IDL_String Methods 01 STRING 返回字符串. 02 STRCMP 比较字符串, ...
- A - Combination Lock
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Scroog ...
- ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 27. CICD Azure DevOps
VSTS做持续集成 后来改名叫做Azure Deveps https://azure.microsoft.com/zh-cn/services/devops/ 这是中文的地址 创建一个项目 名称.描述 ...
- Linux SecureCRT 完全破解
相关说明: 上篇发了个Linux(Ubuntu) 下 SecureCRT 7 30天循环破解在启动的时候会多输入一次确认窗口, 后来maz-1网友留言说可以用Windows破解后程序替换Linux下的 ...
- 牛客 - 17968 - xor序列 - 线性基
https://ac.nowcoder.com/acm/problem/17968 下面是错误的做法,因为题目要求必须使用x,而y在check的时候不一定用到等价于x的线性基来构成. 正确的做法是直接 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)【A,B,C,D】
呵呵哒,上分~ CodeForces 724A: 题意: 给你两个星期几,问连续两个月的头一天是否满足: #include <iostream> #include <stdio.h& ...
- 题解 P1162 【填涂颜色】
看到题目规模是n(1≤n≤30)即最大规模为30*30 本蒟蒻有个奇妙的想法!! 核心思路:搜索地图内除开被1包围着的0,并标注为1(即不填色) !!!那么,我们可以从每一个边界点开始去搜索 话不多说 ...
- 01 | Jewels and Stones
Question You're given strings J representing the types of stones that are jewels, and S representing ...