Ternary String (Codeforces 1354B)
Tags: Implement, two pointers

You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$.

Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) — the number of test cases.Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3.The sum of lengths of all strings in all test cases does not exceed $$$200000$$$.

Output: For each test case, print one integer — the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead.

Note: Consider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used.

Sample Inputs:
7
123
12222133333332
112233
332211
12121212
333333
31121

Sample Outputs:
3
3
4
4
0
0
4

Solution (Python):
from sys import stdin, stdout, maxsize

def get_ternary_string(input_text: str) -> int:
    index = [0] * 3
    count = maxsize
    left = 0
    for right in range(0, len(input_text)):
        pos = ord(input_text[right]) - 49
        index[pos] += 1
        while index[ord(input_text[left]) - 49] > 1:
            index[ord(input_text[left]) - 49] -= 1
            left += 1
        if index[0] != 0 and index[1] != 0 and (index[2] != 0):
            count = min(count, right - left + 1)
    return 0 if count == maxsize else count
test_cases = int(stdin.readline().strip())
while test_cases > 0:
    input_text = stdin.readline().strip()
    print(get_ternary_string(input_text))
    test_cases -= 1

Solution (Python):
from sys import stdin, stdout, maxsize

def get_ternary_string(input_text: str) -> int:
    index = [0] * 3
    count = maxsize
    left = 0
    for right in range(0, len(input_text)):
        pos = ord(input_text[right]) - 49
        index[pos] += 1
        while index[ord(input_text[left]) - 49] > 1:
            index[ord(input_text[left]) - 49] -= 1
            left += 1
        if index[0] != 0 and index[1] != 0 and (index[2] != 0):
            count = min(count, right - left + 1)
    return 0 if count == maxsize else count
test_cases = int(stdin.readline().strip())
while test_cases > 0:
    input_text = stdin.readline().strip()
    print(get_ternary_string(input_text))
    test_cases -= 1

Solution (Python):
def main():
    s = input()
    if len(set(s)) < 3:
        print(0)
        return
    l = [s[0]]
    c = 0
    mini = 10000000000
    i = 1
    while True:
        if s[i] != l[0]:
            l.append(s[i])
            c = i - 1
            index = i
            break
        i += 1
    for i in range(index + 1, len(s)):
        if s[i] == l[0]:
            c = i - 1
            l.pop(0)
            l.append(s[i])
        elif s[i] not in l:
            mini = min(mini, i - c + 1)
            l.append(s[i])
            c = i - 1
            l.pop(0)
            if mini == 3:
                break
    print(mini)
for i in range(int(input())):
    main()