5 Best Ways to Find the Length of the Longest Substring with Character Count of At Least K in Python

Rate this post

πŸ’‘ Problem Formulation: The task is to identify the longest substring within a given string where each character appears at least k times. For example, given the input string “aabbcc” and k = 2, the longest valid substring would be “aabbcc” itself since all characters meet the frequency criteria. However, for k = 3, the result would be an empty substring since no character appears three times.

Method 1: Brute Force Approach

The brute force approach involves iterating through all possible substrings of the given string and checking if each character’s frequency is at least k. This method has a high time complexity, making it less efficient for long strings but straightforward to understand and implement.

Here’s an example:

def longest_substring(s, k):
    max_length = 0
    for start in range(len(s)):
        for end in range(start + 1, len(s) + 1):
            substring = s[start:end]
            if all(substring.count(char) >= k for char in set(substring)):
                max_length = max(max_length, end - start)
    return max_length

print(longest_substring("aabbcc", 2))

Output: 6

This code snippet defines a function longest_substring which takes a string s and an integer k. It initializes a variable max_length to keep track of the length of the longest valid substring found so far. Two nested loops generate all possible substrings, and an inner condition checks if each character appears at least k times. If so, it updates max_length accordingly.

Method 2: Sliding Window Technique

The sliding window technique dynamically adjusts the start and end indices of the substring being checked, thereby reducing the number of iterations as compared to the brute force. This method is much faster and suitable for larger strings, providing a better time complexity.

Here’s an example:

def longest_substring(s, k):
    max_length = 0
    for unique_chars in range(1, len(set(s)) + 1):
        counts = {}
        start = end = 0
        while end < len(s):
            if len(counts) = k for count in counts.values()):
                max_length = max(max_length, end - start)
    return max_length

print(longest_substring("aaabbbbcc", 3))

Output: 7

This code implements the sliding window technique by maintaining a dictionary counts to store the frequency of each character in the current window. It loops over the number of unique characters in the string to adjust the window size. The start and end pointers advance accordingly, checking if the substring satisfies the condition that each character count is at least k and updating max_length when a longer valid substring is found.

Method 3: Divide and Conquer

The divide and conquer approach splits the string on a character that does not meet the frequency condition and recursively checks the resulting substrings. This method leverages recursion to simplify the problem at each step and can be more efficient than the brute force for certain inputs.

Here’s an example:

def longest_substring(s, k):
    if len(s) < k:
        return 0
    for char in set(s):
        if s.count(char) < k:
            return max(longest_substring(sub, k) for sub in s.split(char))
    return len(s)

print(longest_substring("ababbc", 2))

Output: 5

The function longest_substring first checks if the string is shorter than k, returning 0 if so. It iterates over the unique characters of the string, and if any character’s count is less than k, it splits the string on that character and recursively finds the longest valid substring. If all characters meet the condition, it returns the length of the entire string.

Method 4: Hashmap & Two Pointers

This method combines hashmap data structure with two pointer technique to create a dynamic scanning of the string, identifying valid substrings while keeping track of character counts. It is a more sophisticated version of the sliding window and more efficient in terms of space.

Here’s an example:

from collections import defaultdict

def longest_substring(s, k):
    max_length = 0
    start = 0
    count_map = defaultdict(int)
    for end in range(len(s)):
        count_map[s[end]] += 1
        while min(count_map.values()) < k:
            count_map[s[start]] -= 1
            if count_map[s[start]] == 0:
                del count_map[s[start]]
            start += 1
        max_length = max(max_length, end - start + 1)
    return max_length

print(longest_substring("bbaaacbd", 3))

Output: 3

In this code snippet, a defaultdict is used to maintain the count of characters efficiently. The for loop iterates over the string and updates the count of the current character. If the minimum count in the count_map falls below k, the start pointer is advanced while the necessary adjustments are made to the count_map. The max_length is updated when valid substrings are found.

Bonus One-Liner Method 5: Functional Programming

Employing the power of Python’s functional programming capabilities, we can condense the logic of finding the longest substring into a more compact and sometimes more readable one-liner. Note that this method favors brevity over efficiency.

Here’s an example:

longest_substring = lambda s, k: max((len(sub) for sub in s.split(min(s, key=s.count)) if sub.count(sub[0]) >= k), default=0)

print(longest_substring("aacbbbdc", 2))

Output: 3

The one-liner defines a lambda function using a generator expression within the max function. It splits the input string by the least frequent character and then filters substrings ensuring that the count of their first character is at least k. It returns the length of the longest valid substring or 0 by default if none exist.


  • Method 1: Brute Force Approach. Simple to implement. It has a high time complexity and is not efficient for larger inputs due to extensive iterations.
  • Method 2: Sliding Window Technique. Efficiently handles varying window sizes. It can be faster than brute force for long strings, but the code complexity is slightly higher.
  • Method 3: Divide and Conquer. Leverages recursion for a clearer and sometimes more efficient algorithm in certain cases. However, it might lead to a higher space complexity because of recursive calls.
  • Method 4: Hashmap & Two Pointers. Space-efficient and quick for dynamic scanning. Offers optimized space and time complexity, but implementation may be trickier for beginners.
  • One-Liner Method 5: Functional Programming. It offers an extremely compact solution. This approach is less efficient and can be harder to understand for those not familiar with functional programming paradigms.