#C4841. Smallest Segment Length
Smallest Segment Length
Smallest Segment Length
You are given a string that represents a sequence of resources. The string consists of the letters G
, S
, and C
. Your task is to find the length of the smallest contiguous segment of the string which contains at least one of each character: G
, S
, and C
.
If there is no such segment that contains all three characters, your program should output 0
.
Note: The answer must be computed using efficient techniques since the input string can be long. The expected solution uses the sliding window algorithm.
Mathematically, if we denote the string as \(s\) and the window indices as \(i\) and \(j\), then we want: \[ \min_{i,j} \{ j-i+1 \ \text{s.t.} \ \{G,S,C\} \subseteq \{s_i, s_{i+1},\dots,s_j\} \} \]
inputFormat
The input is read from stdin and consists of a single line that contains a non-empty string. This string only consists of the uppercase letters G
, S
, and C
.
outputFormat
The output should be printed to stdout as a single integer denoting the length of the smallest segment that contains at least one G
, one S
, and one C
. If no such segment exists, output 0
.
GSCCGSG
3