BackmediumStringsPhonePe

Longest Substring with K Unique Characters Solution

Problem Statement

Given a string s and an integer k, find the length of the longest substring that contains exactly k unique characters.

Example 1
Input
abba
Output
2

Explanation: Step-by-step: with input 'abba', we initialize the sliding window [0, 0] with 1 unique character 'a'. We expand the window to [0, 1] with 2 unique characters 'a', 'b'. The longest substring with 2 unique characters is 'ab'. We continue expanding the window to [1, 2] with 2 unique characters 'b', 'a'. The longest substring with 2 unique characters is still 'ab'. We continue expanding the window to [2, 3] with 2 unique characters 'a', 'b'. The longest substring with 2 unique characters is still 'ab'. We continue expanding the window to [3, 4] with 2 unique characters 'b', 'a'. The longest substring with 2 unique characters is still 'ab'. We cannot expand the window further, so the longest substring with 2 unique characters is 'ab' with length 2.

Example 2
Input
aab
Output
2

Explanation: Step-by-step: with input 'aab', we initialize the sliding window [0, 0] with 1 unique character 'a'. We expand the window to [0, 1] with 2 unique characters 'a', 'b'. The longest substring with 2 unique characters is 'ab'. We continue expanding the window to [1, 2] with 2 unique characters 'b', 'a'. The longest substring with 2 unique characters is still 'ab'. We cannot expand the window further, so the longest substring with 2 unique characters is 'ab' with length 2.

Constraints

  • 1 <= k <= 26
  • 1 <= s.length <= 10^5
  • s consists of only 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 K Unique Characters — Problem Statement & Solution Guide

StringsMediumSliding Window
TimeO(n)
|
SpaceO(k)

Problem Description

Given a string s and an integer k, find the length of the longest substring that contains exactly k unique characters.

Examples

Example 1

Input

abba

Output

2

Explanation: Step-by-step: with input 'abba', we initialize the sliding window [0, 0] with 1 unique character 'a'. We expand the window to [0, 1] with 2 unique characters 'a', 'b'. The longest substring with 2 unique characters is 'ab'. We continue expanding the window to [1, 2] with 2 unique characters 'b', 'a'. The longest substring with 2 unique characters is still 'ab'. We continue expanding the window to [2, 3] with 2 unique characters 'a', 'b'. The longest substring with 2 unique characters is still 'ab'. We continue expanding the window to [3, 4] with 2 unique characters 'b', 'a'. The longest substring with 2 unique characters is still 'ab'. We cannot expand the window further, so the longest substring with 2 unique characters is 'ab' with length 2.

Example 2

Input

aab

Output

2

Explanation: Step-by-step: with input 'aab', we initialize the sliding window [0, 0] with 1 unique character 'a'. We expand the window to [0, 1] with 2 unique characters 'a', 'b'. The longest substring with 2 unique characters is 'ab'. We continue expanding the window to [1, 2] with 2 unique characters 'b', 'a'. The longest substring with 2 unique characters is still 'ab'. We cannot expand the window further, so the longest substring with 2 unique characters is 'ab' with length 2.

Constraints

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

Optimal Approach & Strategy

The optimized approach uses a sliding window technique with a frequency map to track the unique characters in the current window, achieving a time complexity of O(n).

Brute Force Approach

The brute-force approach involves checking all possible substrings of the given string and counting the number of unique characters in each substring, resulting in a time complexity of O(n²).

Verified Code Solutions

JavaScript Solution
Time: O(n)
function longestSubstring(s, k) {
  let maxLen = 0, start = 0, charCount = {}, uniqueChars = 0;
  for (let end = 0; end < s.length; end++) {
    charCount[s[end]] = (charCount[s[end]] || 0) + 1;
    if (charCount[s[end]] === 1) uniqueChars++;
    while (uniqueChars > k) {
      charCount[s[start]]--;
      if (charCount[s[start]] === 0) uniqueChars--;
      start++;
    }
    maxLen = Math.max(maxLen, end - start + 1);
  }
  return maxLen;
}

Asked in Top Tech Interviews

PhonePe

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.