Leetcode 395. Longest Substring with At Least K Repeating Characters Sure, let’s walk through the problem step-by-step.
Given a string s
and an integer k
, return the length of the longest substring of s
such that the frequency of each character in this substring is at least k
.
s
are lowercase English letters.s
will be in the range [1, 10^4]
.k
will be in the range [1, 10^5]
.To solve this problem, we could use a recursive divide-and-conquer approach:
k
, we cannot have any valid substring, so return 0.k
times in the string, we know it cannot be part of the wanted substring. Thus, split the string around these characters and apply the same process recursively on the resulting substrings.k
.Here’s the C++ implementation of the described approach:
#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
class Solution {
public:
int longestSubstring(const std::string& s, int k) {
return longestSubstringHelper(s, 0, s.size(), k);
}
private:
int longestSubstringHelper(const std::string& s, int start, int end, int k) {
if (end - start < k) {
return 0;
}
std::unordered_map<char, int> frequency;
for (int i = start; i < end; ++i) {
++frequency[s[i]];
}
for (int mid = start; mid < end; ++mid) {
if (frequency[s[mid]] < k) {
int nextMid = mid + 1;
while (nextMid < end && frequency[s[nextMid]] < k) {
++nextMid;
}
return std::max(longestSubstringHelper(s, start, mid, k),
longestSubstringHelper(s, nextMid, end, k));
}
}
return end - start;
}
};
int main() {
Solution solution;
std::string s = "aaabb";
int k = 3;
std::cout << "Longest substring length: " << solution.longestSubstring(s, k) << std::endl; // Output: 3 ("aaa")
s = "ababbc";
k = 2;
std::cout << "Longest substring length: " << solution.longestSubstring(s, k) << std::endl; // Output: 5 ("ababb")
return 0;
}
This approach balances clarity and efficiency given the problem constraints.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?