题目链接: https://leetcode.com/problems/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 characters.
For example, Given s = “eceba”
and k = 2,
T is "ece" which its length is 3.
思路: 一个hash表和一个左边界标记. 遍历字符串将其加入到hash表中, 不同字符多于k个了, 就从左边开始删字符. 直到hash表不同字符长度等于k.此时字符串的长度就是当前字符和左边界的距离.
代码如下:
class Solution {
public:
int lengthOfLongestSubstringKDistinct(string s, int k) {
unordered_map hash;
int left = 0, Max = 0;
for(int i =0; i < s.size(); i++)
{
hash[s[i]]++;
while(hash.size()>k)
{
hash[s[left]]--;
if(hash[s[left]]==0) hash.erase(s[left]);
left++;
}
Max = max(Max, i-left+1);
}
return Max;
}
};