#C8772. Smallest Special Substring

    ID: 52791 Type: Default 1000ms 256MiB

Smallest Special Substring

Smallest Special Substring

You are given a string s consisting only of the characters x, y, and z. Your task is to find the length of the smallest contiguous substring that contains at least one occurrence of each of these three characters. If no such substring exists, output -1.

Input Example:

3
xyzzxy
xyz
xxxxx

Output Example:

3
3
-1

Explanation: In the first test case, the substring "zxy" (or any other valid substring) has length 3 and contains all three characters 'x', 'y', and 'z'.

inputFormat

The input is read from stdin and has the following format:

  • The first line contains an integer T representing the number of test cases.
  • Each of the next T lines contains a non-empty string s made up solely of the characters 'x', 'y', and 'z'.

outputFormat

For each test case, output a single line containing the length of the smallest substring of s that contains at least one occurrence of 'x', 'y', and 'z'. If such a substring does not exist, output -1.

## sample
3
xyzzxy
xyz
xxxxx
3

3 -1

</p>