340_Longest_Substring_with_At_Most_K_Distinct_Characters
Longest Substring with At Most K Distinct Characters
Difficulty hard
tags two_pointers unordered_map
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.
这里很自然想到了两个指针来进行操作。 f作为前指针, e作为后指针, [e, f]之间的所有字符所有字符都满足k distinct character的条件。
正确性易证明, f作为迭代前指针, 每次循环我们都寻找了以f作为结尾的最长的满足k不同字符的序列, 那么最长的序列一定包含在其中。
失误的corner case: k==0 的情况疏忽。
unordered_map的使用。
solution 1
class Solution {
public:
int lengthOfLongestSubstringKDistinct(string s, int k) {
unordered_map<char, int> m;
int f = 0, e = 0; // f --> front, e --> end
int res = 0;
if (k==0) return 0;
while (f < s.size()) {
if (m.find(s[f]) != m.end()) {
m[s[f]]++;
} else {
m[s[f]] = 1;
while (m.size()>k && e<f) {
m[s[e]]--;
if (m[s[e]] == 0) m.erase(s[e]);
e++;
}
}
res = max(f - e + 1, res);
f++;
}
return res;
}
};

浙公网安备 33010602011771号