#C11203. Shortest Substring with All Distinct Characters

    ID: 40494 Type: Default 1000ms 256MiB

Shortest Substring with All Distinct Characters

Shortest Substring with All Distinct Characters

You are given a string s consisting of lowercase letters. The task is to find the length of the shortest substring of s that contains every distinct character present in s at least once.

Let \(S\) be the input string and \(D\) be the set of all distinct characters in \(S\). The objective is to find the minimum length \(L\) such that there exists a substring \(S[i \ldots j]\) with \(j-i+1 = L\) and \(D \subseteq \{S[i], S[i+1], \dots, S[j]\}\).

Example 1: For s = "abac", the distinct characters are {a, b, c}. The shortest substring containing all of them is "bac" with length 3.

Example 2: For s = "aabc", the shortest substring covering all distinct characters is "abc" which has length 3.

Your solution should read input from standard input (stdin) and write the answer to standard output (stdout).

inputFormat

The input consists of a single line containing a non-empty string s made up of lowercase letters.

For example:

abac

outputFormat

Output a single integer – the length of the shortest substring of s that contains all the distinct characters present in s.

For the sample input above, the output should be:

3
## sample
abac
3