395. Longest Substring with At Least K Repeating Characters
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
Method: Sliding Window with Constrained Unique Characters
1. Problem Analysis
Solution
Longest Substring with At Least K Repeating Characters in Python
class Solution:
def longestSubstring(self, s: str, k: int) -> int:
max_len = 0
n = len(s)
# Iterate through all possible numbers of unique characters (1 to 26).
for h in range(1, 27):
counts = [0] * 26
start = 0
unique = 0 # Count of unique characters in the current window.
at_least_k = 0 # Count of characters with frequency >= k.
for end in range(n):
# Expand the window by including s[end].
char_in_idx = ord(s[end]) - ord('a')
if counts[char_in_idx] == 0:
unique += 1
counts[char_in_idx] += 1
if counts[char_in_idx] == k:
at_least_k += 1
# Shrink the window if the number of unique characters exceeds h.
while unique > h:
char_out_idx = ord(s[start]) - ord('a')
if counts[char_out_idx] == k:
at_least_k -= 1
counts[char_out_idx] -= 1
if counts[char_out_idx] == 0:
unique -= 1
start += 1
# If the window has h unique characters and all of them appear at least k times,
# update max_len.
if unique == h and at_least_k == h:
max_len = max(max_len, end - start + 1)
return max_len
Longest Substring with At Least K Repeating Characters in Java
class Solution {
public int longestSubstring(String s, int k) {
int maxLen = 0;
int n = s.length();
// Iterate through all possible numbers of unique characters (1 to 26).
for (int h = 1; h <= 26; h++) {
int[] counts = new int[26];
int start = 0;
int unique = 0; // Count of unique characters in the current window.
int atLeastK = 0; // Count of characters with frequency >= k.
for (int end = 0; end < n; end++) {
// Expand the window by including s.charAt(end).
int charInIdx = s.charAt(end) - 'a';
if (counts[charInIdx] == 0) {
unique++;
}
counts[charInIdx]++;
if (counts[charInIdx] == k) {
atLeastK++;
}
// Shrink the window if the number of unique characters exceeds h.
while (unique > h) {
int charOutIdx = s.charAt(start) - 'a';
if (counts[charOutIdx] == k) {
atLeastK--;
}
counts[charOutIdx]--;
if (counts[charOutIdx] == 0) {
unique--;
}
start++;
}
// If the window has h unique characters and all of them appear at least k times,
// update maxLen.
if (unique == h && atLeastK == h) {
maxLen = Math.max(maxLen, end - start + 1);
}
}
}
return maxLen;
}
}
Longest Substring with At Least K Repeating Characters in C++
class Solution {
public:
int longestSubstring(string s, int k) {
int max_len = 0;
int n = s.length();
// Iterate through all possible numbers of unique characters (1 to 26).
for (int h = 1; h <= 26; ++h) {
vector<int> counts(26, 0);
int start = 0;
int unique = 0; // Count of unique characters in the current window.
int at_least_k = 0; // Count of characters with frequency >= k.
for (int end = 0; end < n; ++end) {
// Expand the window by including s[end].
int char_in_idx = s[end] - 'a';
if (counts[char_in_idx] == 0) {
unique++;
}
counts[char_in_idx]++;
if (counts[char_in_idx] == k) {
at_least_k++;
}
// Shrink the window if the number of unique characters exceeds h.
while (unique > h) {
int char_out_idx = s[start] - 'a';
if (counts[char_out_idx] == k) {
at_least_k--;
}
counts[char_out_idx]--;
if (counts[char_out_idx] == 0) {
unique--;
}
start++;
}
// If the window has h unique characters and all of them appear at least k times,
// update max_len.
if (unique == h && at_least_k == h) {
max_len = max(max_len, end - start + 1);
}
}
}
return max_len;
}
};
Longest Substring with At Least K Repeating Characters in TypeScript
function longestSubstring(s: string, k: number): number {
let maxLen = 0;
const n = s.length;
// Iterate through all possible numbers of unique characters (1 to 26).
for (let h = 1; h <= 26; h++) {
const counts = new Array(26).fill(0);
let start = 0;
let unique = 0; // Count of unique characters in the current window.
let atLeastK = 0; // Count of characters with frequency >= k.
for (let end = 0; end < n; end++) {
// Expand the window by including s[end].
const charInIdx = s.charCodeAt(end) - 97; // 'a'.charCodeAt(0) is 97
if (counts[charInIdx] === 0) {
unique++;
}
counts[charInIdx]++;
if (counts[charInIdx] === k) {
atLeastK++;
}
// Shrink the window if the number of unique characters exceeds h.
while (unique > h) {
const charOutIdx = s.charCodeAt(start) - 97;
if (counts[charOutIdx] === k) {
atLeastK--;
}
counts[charOutIdx]--;
if (counts[charOutIdx] === 0) {
unique--;
}
start++;
}
// If the window has h unique characters and all of them appear at least k times,
// update maxLen.
if (unique === h && atLeastK === h) {
maxLen = Math.max(maxLen, end - start + 1);
}
}
}
return maxLen;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.