#K78782. Smallest Window Substring
Smallest Window Substring
Smallest Window Substring
You are given a non-empty string s
. Your task is to find the length of the smallest contiguous substring (window) that contains every distinct character present in s
at least once.
Problem Details:
- The input string may contain repeated characters.
- You must determine the minimal window which covers all unique characters in the string.
- You are encouraged to use efficient techniques such as sliding window and two pointers to achieve optimal time performance.
Mathematical Formulation:
Given a string \(s\) with length \(n\) and let \(U\) be the set of unique characters in \(s\). Find the smallest integer \(L\) such that there exists an index \(i\) with \(0 \leq i \leq n-L\) and the substring \(s[i\ldots i+L-1]\) satisfies \(U \subseteq \{ s[i], s[i+1], \ldots, s[i+L-1] \}\).
Example:
Input: aabcbcdbca Output: 4</p>Explanation: The smallest window that contains all unique characters is "dbca" which has length 4.
inputFormat
The first line of the input contains an integer T
denoting the number of test cases. Each of the following T
lines contains a single string s
.
outputFormat
For each test case, output the length of the smallest window in s
that contains all the distinct characters. Print each result on a separate line.
2
aabcbcdbca
aaab
4
2
</p>