pyassistant

Longest substring with at most k distinct characters

Given a string s and an integer k, return the length of the longest substring of s that contains at most k distinct characters. If k is 0, the answer is 0. The string may be empty. Your solution should run in O(n) time where n = len(s).

Example:

Input:
("eceba", 2)
Output:
3

Make sure you return your solution, don't print!

AI

Bot

Trying to solve my challenge? Ask if you must, or press the purple button so I can analyze your code.