Given a string S consisting of the characters 0, 1 and 2. Your task is to find the length of the smallest substring of string S that contains all the three characters 0, 1 and 2. If no such substring exists, then return -1.
Example 1
Input: S = 10212
Output: 3
Explanation: The substring 102 is the smallest substring that contains the characters 0, 1 and 2.
Example 2
Input: S = 12121
Output: -1
Explanation: As the character 0 is not present in the string S, therefor no substring containing all the three characters 0, 1 and 2 exists. Hence, the answer is -1 in this case.
Sign in to write, run, and submit your solution against the full test suite.