Longest Substring with At Most K Distinct Characters

Finding Longest Subarrays (Standard Sliding Window) DSA practice problem on Onlearn.

Difficulty: medium.

Topics: Longest Substring with At Most K Distinct Characters, Strings, String Manipulation, Two Pointers, Sliding Window, Hash Map, Frequency Counting, Time Complexity, Space Complexity, Optimization, Loops, complexity analysis, sliding window, string properties, frequency counting, two pointer technique, Subarray/Substring Problems.

Longest Substring with At Most K Distinct Characters Problem Statement Given a string s and an integer k, find the length of the longest substring T of s that contains at most k distinct characters. Input Specification The first line contains a string s (1 <= |s| <= 10^5), consisting of lowercase English letters. The second line contains an integer k (0 <= k <= 26). Output Specification Print a single integer, the length of the longest substring meeting the criteria. Constraints s will only contain lowercase English letters. Sample Test Cases Sample Input 1 Sample Output 1 Explanation 1 The longest substring with at most 2 distinct characters is "ece", which has a length of 3. Sample Input 2 Sample Output 2 Explanation 2 The longest substring with at most 1 distinct character is "aaaaa", which has a length of 5.