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- ...
随机推荐
- [YNOI 2016] 掉进兔子洞
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4939 [算法] 不难发现 , ansi = (r1 - l1 + 1) + (r2 ...
- 中缀表达式std
#include<cstdio>#include<cstdlib>#include<string>#include<cstring>using name ...
- C++模板之可变模板参数
可变模板参数---- C++11新特性 可变模板参数(variadic templates)是C++11新增的最强大的特性之一,它对参数进行了高度泛化,它能表示0到任意个数.任意类型的参数 由于可变模 ...
- calico在docker上的部署及验证
1. 背景 以下的部署以五台服务器环境为例: 服务器1: hostname为etcdnode1, IP为192.168.56.100 服务器2: hostname为etcdnode2, IP为192. ...
- set和multiset容器
set和multiset容器的能力 set 和multiset容器的内部结构通常由平衡二叉树(balanced binary tree)来实现.当元素放入容器中时,会按照一定的排序法则自动排序,默认是 ...
- .net 反射构造你自己的“匿名”对象
由于近来项目的底层架构某些特殊需求及场景的需要要求动态build一个对象, 属性名称个类与类型都是外界动态传入的. 不多说废话,直接上我最原始的代码: public static Type GetMy ...
- CodeForces599C【贪心】
题意: 给你一个序列,要求你从小到大排序,你可以划分成一个块一个块地进行块内排序,问你最多能分成几个块 思路: 贪心,首先感觉就是有正序的话我就分开啊: 难道倒序不能分块?321肯定不行啊. 存不存在 ...
- laravel 报错htmlspecialchars() expects parameter 1 to be string, object given
翻译过来就是 期望参数1是字符串 意思就是说变量为数组,应以数组的方式输出 @foreach($xxx as $k=>$y) {{$k}}{{$y}} @endforeach
- unity调用Android的jar包
简介 有一些手机功能,Unity没有提供相应的接口,例如震动,例如不锁屏,例如GPS,例如... 有太多的特殊功能Unity都没有提供接口,这时候,我们就需要通过使用Android原生的ADT编辑器去 ...
- Inside Geometry Instancing(下)
Inside Geometry Instancing(下) http://blog.csdn.net/soilwork/article/details/655858 此教程版权归我所有,仅供个人学习使 ...