#K74587. Smallest Balanced Window
Smallest Balanced Window
Smallest Balanced Window
Given a string s
consisting solely of the characters X
and Y
, your task is to find the length of the smallest contiguous substring (window) that contains an equal number of X
's and Y
's. Formally, if we define the net count as \(d(i) = \#X - \#Y\) in the substring, you need to find a window where the net count is 0.
If no such window exists, your program should output -1
.
For example, given the string "XXXYXYX", the smallest window that balances the numbers of X
and Y
is of length 2.
inputFormat
The input consists of a single line containing a non-empty string s
which is made up only of the characters X
and Y
.
Input Format:
s
outputFormat
Output a single integer which represents the length of the smallest substring (window) that has an equal number of X
and Y
. If no such substring exists, output -1
.
Output Format:
result## sample
XXXYXYX
2