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 greater than or equal to k
.
if no such substring exists, return 0.
Example 1:
Input: s = "aaabb", k = 3
Output: 3
Explanation: The longest substring is "aaa", as 'a' is repeated 3 times.
Example 2:
Input: s = "ababbc", k = 2
Output: 5
Explanation: The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.
Constraints:
1 <= s.length <= 10^4
s
consists of only lowercase English letters.1 <= k <= 10^5
# Longest Substring with At Least K Repeating Characters
## Problem Description
Given a string `s` and an integer `k`, the task is to find the length of the longest substring of `s` such that the frequency of each character in this substring is greater than or equal to `k`. If no such substring exists, the function should return 0.
**Example 1:**
Input: s = "aaabb", k = 3 Output: 3 Explanation: The longest substring is "aaa", as 'a' is repeated 3 times.
**Example 2:**
Input: s = "ababbc", k = 2 Output: 5 Explanation: The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.
## Brute Force Solution
A naive approach would involve generating all possible substrings of the input string `s` and, for each substring, checking if the frequency of each character is at least `k`. We would then keep track of the longest substring that satisfies this condition.
```python
def longestSubstring_brute_force(s, k):
n = len(s)
max_len = 0
for i in range(n):
for j in range(i, n):
sub = s[i:j+1]
counts = {}
for char in sub:
counts[char] = counts.get(char, 0) + 1
valid = True
for char in counts:
if counts[char] < k:
valid = False
break
if valid:
max_len = max(max_len, len(sub))
return max_len
A more efficient solution uses a divide-and-conquer approach. The idea is to find characters in the string whose frequency is less than k
. We then split the string at these characters and recursively solve the subproblems for the substrings obtained.
def longestSubstring(s, k):
n = len(s)
if n == 0 or k > n:
return 0
if k <= 1:
return n
counts = {}
for char in s:
counts[char] = counts.get(char, 0) + 1
for i in range(n):
if counts[s[i]] < k:
left = longestSubstring(s[:i], k)
right = longestSubstring(s[i+1:], k)
return max(left, right)
return n
s
. This is because we generate all possible substrings (O(n^2)) and, for each substring, we iterate through it to count character frequencies (O(n)).s
. This is due to the space used to store the substrings and the character counts.s
. This is due to the recursion stack, which can grow up to n in the worst case (when the string is repeatedly split into single characters). Also, we are using a dictionary that stores character frequencies that can be maximum of size O(26) which is O(1). The recursion stack dominates the space complexity giving us O(n).s
is empty, the function should return 0.k
is greater than the length of the string s
, no substring can satisfy the condition, and the function should return 0.k
is less than or equal to 1, any substring satisfies the condition, and the function should return the length of the string s
.