Problem Statement:


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.

Solution:


Prerequisite: Sliding Window

Algorithm:



Login to Access Content




Java Code:



Login to Access Content




Python Code:



Login to Access Content




Instructor:



If you have any feedback, please use this form: https://thealgorists.com/Feedback.



Help Your Friends save 40% on our products

wave