#K61677. Minimum Length Substring with All Distinct Characters

    ID: 31363 Type: Default 1000ms 256MiB

Minimum Length Substring with All Distinct Characters

Minimum Length Substring with All Distinct Characters

You are given a string s. Your task is to find the length of the smallest substring of s that contains all the distinct characters present in s. Formally, if we denote by \( S \) the input string and by \( D \) the set of distinct characters in \( S \), you should 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 \ldots j] \). If the string is empty, the answer is 0.

Example:

  • Input: "abac"
    Output: 3, because the substring "bac" (or "aba") is the smallest substring that contains the characters a, b, and c.

inputFormat

The input consists of a single line containing a non-empty string s which may include letters and/or other characters. If the string is empty, output will be 0.

outputFormat

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

## sample
abac
3