Longest Substring with At Least K Repeating Characters - Complete Solution Guide
Longest Substring with At Least K Repeating Characters is LeetCode problem 395, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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
Detailed Explanation
The problem asks us to find the longest substring within a given string 's' such that every character in that substring appears at least 'k' times. If no such substring exists, we should return 0. The input consists of a string 's' and an integer 'k'. The string contains only lowercase English letters, and the integer 'k' represents the minimum frequency a character must have in a valid substring. The output is the length of the longest valid substring.
Solution Approach
The solution employs a sliding window technique combined with an outer loop that iterates through possible numbers of unique characters in the substring. For each possible number of unique characters, the sliding window expands and contracts to maintain a substring with that specific number of unique characters. During the window's movement, it checks if all the characters within the window appear at least 'k' times. If a substring meets both conditions (the desired number of unique characters and each character appearing at least 'k' times), the solution updates the maximum length found so far.
Step-by-Step Algorithm
- Step 1: Calculate the number of unique characters in the input string 's'. This value will limit the maximum number of unique characters we need to consider in our substrings.
- Step 2: Iterate through possible numbers of unique characters ('h') in the substring, from 1 to the total number of unique characters.
- Step 3: Initialize a sliding window with 'left' and 'right' pointers, a character count map, a 'unique_count' variable (number of unique characters in the window), and an 'at_least_k_count' variable (number of unique characters appearing at least 'k' times in the window).
- Step 4: Expand the sliding window by moving the 'right' pointer one step at a time. Update the character count map, 'unique_count', and 'at_least_k_count' accordingly.
- Step 5: While the number of unique characters in the window ('unique_count') exceeds the current target number of unique characters ('h'), contract the sliding window by moving the 'left' pointer one step at a time. Update the character count map, 'unique_count', and 'at_least_k_count' accordingly.
- Step 6: If the number of unique characters in the window ('unique_count') equals 'h' and the number of characters appearing at least 'k' times ('at_least_k_count') also equals 'h', then update the maximum length found so far.
- Step 7: After iterating through all possible 'right' pointer positions, repeat steps 3-6 for the next value of 'h'.
- Step 8: Return the maximum length found.
Key Insights
- Insight 1: The problem can be approached using a sliding window. However, a simple sliding window isn't directly applicable because we need to ensure *all* characters meet the frequency requirement.
- Insight 2: The solution iterates through possible numbers of unique characters (from 1 to the total number of unique characters in the string). For each possible number of unique characters, it uses a sliding window to find the longest substring containing that number of unique characters, where each of those characters appears at least k times.
- Insight 3: By considering different numbers of unique characters ('h') and checking if a substring with 'h' unique characters and each character appearing at least 'k' times exists, we can ensure we find the maximum length among all such valid substrings.
Complexity Analysis
Time Complexity: O(N)
Space Complexity: O(1)
Topics
This problem involves: Hash Table, String, Divide and Conquer, Sliding Window.
Companies
Asked at: Baidu, Yandex.