Longest Substring with At Most K Distinct Characters
Given a string s and an integer k, return the length of the longest contiguous substring that contains at most k distinct characters.
Given a string `s` and an integer `k`, return the length of the longest contiguous substring that contains at most `k` distinct characters.
- `1 <= s.length <= 10^5` - `s` contains lowercase English letters - `0 <= k <= 26` - The interactive visualizer accepts at most 20 characters; longer valid strings are execution-only.
Example 1: input s = "eceba", k = 2; output 3. The substring "ece" has two distinct characters and length three.
Example 2: input s = "aa", k = 1; output 2. The whole string contains one distinct character.
Category: Sliding Window. Topic: sliding-window. Difficulty: Medium. Patterns: sliding-window, hashing.