BackmediumStringsAmazonTCS

Longest Substring with Limited Frequency Diversity Solution

Problem Statement

Given a string s and an integer k, return the maximum length of a substring of s such that the number of distinct frequencies of characters in the substring is at most k.

Example 1
Input
aab, k = 2
Output
2

Explanation: Step-by-step: with input 'aab' and k = 2, we can see that the substring 'aa' has a frequency of {2}, and the substring 'ab' has a frequency of {1, 1}. Both of these substrings have at most 2 distinct frequencies, so the maximum length of a valid substring is 2.

Example 2
Input
abaccc, k = 1
Output
6

Explanation: Step-by-step: with input 'abaccc' and k = 1, we can see that the substring 'abaccc' has a frequency of {1, 1, 1, 1, 1, 1}, which has only 1 distinct frequency. Therefore, the maximum length of a valid substring is 6.

Constraints

  • 1 <= s.length <= 5 * 10^4
  • 1 <= k <= 26
  • s consists only of lowercase English letters.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Longest Substring with Limited Frequency Diversity — Problem Statement & Solution Guide

StringsMediumSliding Window
TimeO(n)
|
SpaceO(n)

Problem Description

Given a string s and an integer k, return the maximum length of a substring of s such that the number of distinct frequencies of characters in the substring is at most k.

Examples

Example 1

Input

aab, k = 2

Output

2

Explanation: Step-by-step: with input 'aab' and k = 2, we can see that the substring 'aa' has a frequency of {2}, and the substring 'ab' has a frequency of {1, 1}. Both of these substrings have at most 2 distinct frequencies, so the maximum length of a valid substring is 2.

Example 2

Input

abaccc, k = 1

Output

6

Explanation: Step-by-step: with input 'abaccc' and k = 1, we can see that the substring 'abaccc' has a frequency of {1, 1, 1, 1, 1, 1}, which has only 1 distinct frequency. Therefore, the maximum length of a valid substring is 6.

Constraints

  • 1 <= s.length <= 5 * 10^4
  • 1 <= k <= 26
  • s consists only of lowercase English letters.

Optimal Approach & Strategy

Using a sliding window with two maps (one for char frequencies, one for frequency-of-frequencies) allows constant time updates as the window expands and shrinks. This reduces the time complexity to O(n) because each character is added and removed from the window at most once.

Brute Force Approach

The brute-force approach involves generating all possible substrings using nested loops, which results in O(n^2) substrings. For each substring, you calculate the frequency of characters and check if the count of distinct frequencies is less than or equal to k, leading to an overall O(n^3) complexity.

Verified Code Solutions

No solution currently available for JavaScript. Select another language tab above.

Asked in Top Tech Interviews

AmazonTCS

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.