← 返回 meta 的题目列表Shortest Substring with N Unique Letters
类型:online_judge
Given a string and an integer n, find the shortest substring that contains exactly n different characters.
Input:
A string s with length L, where 1 <= L <= 10^5.
An integer n, where 1 <= n <= 26.
Output:
Return an integer indicating the length of the shortest substring that meets the requirement. If there is no such substring, return -1.
Example:
Input: s = 'abcabcbb', n = 2
Output: 2
Explanation: The shortest substring is 'ab' or 'bc', with length 2.
Input: s = 'aa', n = 1
Output: 1
Explanation: The shortest substring is 'a', with length 1.
Input: s = 'ab', n = 3
Output: -1
Explanation: There is no substring with 3 different characters.
Example
Input
abcabcbb
2