395. Longest Substring with At Least K Repeating Characters
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/
/*
* @lc app=leetcode id=395 lang=cpp
*
* [395] Longest Substring with At Least K Repeating Characters
*/
// @lc code=start
class Solution {
public:
int longestSubstring(string s, int k) {
int l = 0, r = 0, sat = 0, res = 0;
unordered_map<char, int> freqs;
for (int cnt = 1; cnt <= 26; ++cnt) {
freqs.clear();
l = r = sat = 0;
while (r < s.length()) {
const auto key = s[r];
++freqs[key];
if (freqs[key] == k) ++sat;
while (freqs.size() > cnt) {
const auto lk = s[l];
if (freqs[lk] == k) --sat;
--freqs[lk];
++l;
if (freqs[lk] == 0) freqs.erase(lk);
}
if (sat == cnt) res = max(res, r - l + 1);
++r;
}
}
return res;
}
};
// @lc code=end
Last updated