Longest Substring with At Most K Distinct Characters
Given a string s and an integer k, return the length of the longest substring of s that contains at most k distinct characters.

30:00

Longest Substring with At Most K Distinct Characters
medium
Topics
Companies

Given a string s and an integer k, return the length of the longest substring of s that contains at most k distinct characters.

Example 1:
Input: {"s":"eceba","k":2}
Output: 3
Constraints:
  • 1s.length5×1041 \leq s.\text{length} \leq 5 \times 10^4

  • 0k500 \leq k \leq 50

Input
arr ={"s":"eceba","k":2}

Find longest substring with at most 2 distinct characters

e

0

c

1

e

2

b

3

a

4

left=0

right=-1

Character Count Map (k=2)

empty

distinct = 0 k = 2

Window Length:

0

Max Length:

0

Pointers

Valid Window

Invalid Window

Variables
No variables to display
DepthFunction Call
Stack empty
0/6