Good String (Codeforces 1389C)
Tags: Greedy, two pointers

Let's call left cyclic shift of some string $$$t_1 t_2 t_3 \dots t_{n - 1} t_n$$$ as string $$$t_2 t_3 \dots t_{n - 1} t_n t_1$$$.Analogically, let's call right cyclic shift of string $$$t$$$ as string $$$t_n t_1 t_2 t_3 \dots t_{n - 1}$$$.Let's say string $$$t$$$ is good if its left cyclic shift is equal to its right cyclic shift.You are given string $$$s$$$ which consists of digits 0–9.What is the minimum number of characters you need to erase from $$$s$$$ to make it good?

Input: The first line contains single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.Next $$$t$$$ lines contains test cases — one per line. The first and only line of each test case contains string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$). Each character $$$s_i$$$ is digit 0–9.It's guaranteed that the total length of strings doesn't exceed $$$2 \cdot 10^5$$$.

Output: For each test case, print the minimum number of characters you need to erase from $$$s$$$ to make it good.

Note: In the first test case, you can erase any $$$3$$$ characters, for example, the $$$1$$$-st, the $$$3$$$-rd, and the $$$4$$$-th. You'll get string 51 and it is good.In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good.In the third test case, the given string $$$s$$$ is already good.

Sample Inputs:
3
95831
100120013
252525252525

Sample Outputs:
3
5
0

Solution (Python):
import re
for t in range(int(input())):
    n = input()
    set_s = set(n)
    mx = max((n.count(i) for i in set_s))
    for i in set_s:
        for j in set_s:
            if i == j:
                continue
            else:
                lxm = 2 * len(re.findall(f'{i}[^{i}]*{j}', n))
                mx = max(mx, lxm)
    print(len(n) - mx)

Solution (Python):
from collections import Counter

def main():
    return '\n'.join(map(str, (nCharactersToEraseFrom(input()) for _ in range(int(input())))))

def nCharactersToEraseFrom(string):
    repetition = Counter(string)
    longestLength1 = repetition.most_common(1)[0][1]
    halfLength = longestLength1 // 2
    letters = tuple(set(string))
    if len(letters) > 1:
        longestLength2 = longestLength1
        for i in range(len(letters) - 1):
            if repetition[letters[i]] > halfLength:
                for j in range(i + 1, len(letters)):
                    if repetition[letters[j]] > halfLength:
                        l = goodStringLengthFrom(string, {letters[i], letters[j]})
                        if l > longestLength2:
                            longestLength2 = l
        if longestLength2 != longestLength1:
            return len(string) - (longestLength2 - 1 if longestLength2 % 2 else longestLength2)
    return len(string) - longestLength1

def goodStringLengthFrom(string, pairOfLetters):
    currLetter = 0
    length = 0
    for l in string:
        if l in pairOfLetters and l != currLetter:
            currLetter = l
            length += 1
    return length
if __name__ == '__main__':
    print(main())

Solution (Python):
for s in [*open(0)][1:]:
    a = [0] * 100
    for i in map(int, s[:-1]):
        for j in range(10):
            a[10 * i + j] = a[10 * j + i] + 1
    print(len(s) - 1 - max(max(a) & ~1, *a[::11]))