Question
Find Longest Substring
You are given a string
Find the length of the longest contiguous substring of
Input
The first line contains the string KaTeX can only parse string typed expression (this line may be empty).
The second line contains a single integer KaTeX can only parse string typed expression.
Output
Print a single integer — the length of the longest substring containing at most KaTeX can only parse string typed expression distinct characters.
Example
Example 1:
Input
The substring "ece" (indices 0-2) has exactly 2 distinct characters ('e' and 'c') and length 3, which is the longest such substring.
Example 2:
Input
With at most 1 distinct character allowed, the longest valid substring is any single run of the same repeated character, such as "aaa", "bbb", or "ccc", each of length 3.
Example 3:
Input
With KaTeX can only parse string typed expression, no substring is allowed to contain any character at all, so the only valid substring is the empty one, with length 0.
Input
eceba
2Output3ExplanationThe substring "ece" (indices 0-2) has exactly 2 distinct characters ('e' and 'c') and length 3, which is the longest such substring.
Example 2:
Input
aaabbbccc
1Output3ExplanationWith at most 1 distinct character allowed, the longest valid substring is any single run of the same repeated character, such as "aaa", "bbb", or "ccc", each of length 3.
Example 3:
Input
abcabc
0Output0ExplanationWith KaTeX can only parse string typed expression, no substring is allowed to contain any character at all, so the only valid substring is the empty one, with length 0.