#K68442. Smallest Window with All Distinct Characters
Smallest Window with All Distinct Characters
Smallest Window with All Distinct Characters
Given a string s
, find the length of the smallest contiguous substring (window) that contains all the distinct characters present in s
. In other words, if we denote by \(D\) the set of distinct characters in s
, you need to find the minimum length of a substring such that every character in \(D\) appears at least once in that substring.
Example:
- If
s = "aabcbcdbca"
, the smallest window that contains all distinct characters is "dbca" and its length is 4. - If
s = "aaab"
, the answer is 2.
The challenge is to efficiently determine this minimum window length. A common approach uses a sliding window technique along with a frequency count to keep track of the characters within the window.
inputFormat
The input consists of a single line containing the string s
(with no spaces) for which the smallest window length is to be found.
You can assume that s
is non-empty.
outputFormat
Print a single integer representing the length of the smallest contiguous substring of s
that contains all its distinct characters.
aabcbcdbca
4