03. Longest Substring with K Uniques
β GFG solution to the Longest Substring with K Uniques problem: find maximum length substring containing exactly k distinct characters using sliding window technique. π
π§© Problem Description
π Examples
Example 1
Input: s = "aabacbebebe", k = 3
Output: 7
Explanation: The longest substring with exactly 3 distinct characters is "cbebebe",
which includes 'c', 'b', and 'e'.Example 2
Input: s = "aaaa", k = 2
Output: -1
Explanation: There's no substring with 2 distinct characters.Example 3
π Constraints
β
My Approach
Sliding Window + Frequency Array
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
π§βπ» Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Previous02. Longest Subarray with At Most Two Distinct IntegersNext04. Subarrays With At Most K Distinct Integers
Last updated