LeetCode "Longest Substring with At Most K Distinct Characters"
A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical sliding window problem.
class Solution {
public:
int lengthOfLongestSubstringKDistinct(string s, int k) {
unordered_map<char, unsigned> hm; int ret = , start = ;
for (int i = ; i < s.length(); i++)
{
hm[s[i]]++; if (hm.size() <= k)
{
ret = std::max(ret, i - start + );
}
else
{
while (start < i && hm.size() > k)
{
char nc = s[start];
if (hm[nc] == ) hm.erase(nc);
else hm[nc]--; start++;
}
}
}
return ret;
}
};
LeetCode "Longest Substring with At Most K Distinct Characters"的更多相关文章
- [LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- Leetcode: Longest Substring with At Most K Distinct Characters && Summary: Window做法两种思路总结
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- [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] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
Given a string S, find the length of the longest substring T that contains at most two distinct char ...
- LeetCode Longest Substring with At Most Two Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/ 题目: Gi ...
- [leetcode]340. Longest Substring with At Most K Distinct Characters至多包含K种字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- LeetCode 340. Longest Substring with At Most K Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...
- [LeetCode] 340. Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- [Swift]LeetCode340.最多有K个不同字符的最长子串 $ Longest Substring with At Most K Distinct Characters
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
随机推荐
- Lua标准库(转)
转载地址:http://www.yiibai.com/lua/lua_standard_libraries.html Lua的标准库提供了一组丰富的功能,与C的API直接实现,建立在Lua编程语言函数 ...
- java项目上各种小问题
md,出现好几次这种问题,还得上百度? 以此为证,再出现这种问题我就不信想不起来怎么解决!!!----清除不存在jar包 ok,第二个问题: 每个类上有无数个红叉,然而代码并没有问题 解决方案:run ...
- Pythono 实现 Permutation
不管在R 还是python中,都有现成的函数来轻而易举地进行全排列(Permutation).无序排列等等.今天想要尝试一下使用自己写代码来实现全排列. 首先,我采用的算法如下: 对于一个数列 i.e ...
- 精美的HTML5 Loadding页面
以前我们大部分的Loading动画都是利用gif图片实现的,这种图片实现Loading动画的方法虽然也很不错,但是作为HTML5开发者来说,如果能利用HTML5和CSS3实现这些超酷的Loading动 ...
- session处理超时的三种方式
1. 在web容器中设置(此处以tomcat为例) 在tomcat-5.0.28\conf\web.xml中设置,以下是tomcat 5.0中的默认配置: <!-- ========= ...
- UVa 442 矩阵链乘(栈)
Input Specification Input consists of two parts: a list of matrices and a list of expressions. The f ...
- URAL Mosaic(并查集)(欧拉回路)
Mosaic Time limit: 0.25 secondMemory limit: 64 MB There's no doubt that one of the most important an ...
- POJ 1469 COURSES(二部图匹配)
COURSES Time Limit: 1000MS Memory ...
- Windows7下QT5开发环境搭建 分类: QT开发 2015-03-09 23:44 65人阅读 评论(0) 收藏
Windows7下QT开法环境常见搭配方法有两种. 第一种是:QT Creator+QT SDK: 第二种是:VS+qt-vs-addin+QT SDK: 以上两种均可,所需文件见QT社区,QT下载地 ...
- MFC-CString 字符串分割
CString strSrc = _T("1++2+3+4"); CStringArray strResult; CString strGap = _T("+" ...