Kitty
InterviewTime limit1sMemory limit512 MB
Find the longest contiguous substring with at most N distinct lowercase letters.
- Level
Medium4 of 10
- Topics
- Sliding window, Two pointers, Hash map
- Solved
- No attempts yet
Problem
Cats are so cute. People adored cats so much that they decided to build a cat language translator to communicate with them. This translator will turn human language into cat language and cat language into human language, an invention unlike any other.
A beta version of the cat language translator is out now. But this beta is a complete mess. Given a string, the beta translator recognizes only a contiguous substring that contains at most N distinct letters. It is a poor showing, but people thought it was the best they could do. They also want to know the length of the longest substring this translator can recognize for a given string.
Let us work together so we can communicate with cats.
Input
The first line contains N, the maximum number of distinct letters the translator can recognize. (1 < N ≤ 26)
The second line contains a string. (1 ≤ length of the string ≤ 100,000) The string contains only lowercase English letters.
Output
Print the maximum length of a string the translator can recognize.
Hint
For abbcaccba, the answer is cacc. The translator recognizes at most 2 distinct letters, and the longest contiguous substring with at most 2 distinct letters is cacc. So the answer is the length of cacc, which is 4.