#K44517. Shortest Substring with All Nucleotides
Shortest Substring with All Nucleotides
Shortest Substring with All Nucleotides
Given a DNA sequence string s
, find the length of the shortest contiguous substring that contains at least one occurrence of each nucleotide from the set \(\{A, T, C, G\}\). If no such substring exists, output 0.
Example: For the input ATCGTTCA
, the shortest substring that contains all nucleotides is ATCG
which has length 4.
This problem requires you to implement an efficient algorithm, typically using the sliding window technique.
inputFormat
The input consists of a single line containing a non-empty string s
composed of uppercase letters. The string represents a DNA sequence and may only include the characters A
, T
, C
, and G
, though it might include other characters as well. Read the input from standard input (stdin).
outputFormat
Output a single integer which is the length of the shortest contiguous substring of s
that contains at least one occurrence of each nucleotide in \(\{A, T, C, G\}\). If such a substring doesn’t exist, output 0. Write the output to standard output (stdout).
ATCGTTCA
4