- Difficulty: Medium
- Tags: LeetCode, Medium, Hash Table, String, Sliding Window, leetcode-340, O(n), O(1), Hard, 🔒, LintCode, Hash Tables
Problem
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 Explanation: The substring is "ece" with length 3.
Example 2:
Input: s = "aa", k = 1 Output: 2 Explanation: The substring is "aa" with length 2.
Â
Constraints:
1 <= s.length <= 5 * 104
0 <= k <= 50