Loading...
Loading...
Loading...
A keyboard test counts how many different letters are used in a stretch of text. Given a lowercase text and an integer k, print the length of the longest piece that has at most k different letters.
Example
Input
eceba 2
Output
3
The piece "ece" has two different letters and length 3.
Try the problem first. Open one hint at a time.
Count the letters in the current piece with a dictionary or Counter.
While the dictionary has more than k keys, remove the left letter and delete its key when the count is 0.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor