[2, 3, 4, 5, 6, 1]
[6, 1, 2, 3, 4, 5]
def len_merged(lhs, rhs):
if len(lhs) == 0 or len(rhs) == 0:
return 0
if lhs[0] > rhs[0]:
lhs, rhs = rhs, lhs
i, j = 0, 0
prev_c = 'r'
s_len = 0
while i < len(lhs) and j < len(rhs):
if prev_c == 'r': if rhs[j] < lhs[i]:
j += 1
else:
prev_c = 'l'
s_len += 1
elif prev_c == 'l':
if lhs[i] < rhs[j]:
i += 1
else:
prev_c = 'r'
s_len += 1
if (i == len(lhs) and lhs[-1] < rhs[-1]) or (j == len(rhs) and rhs[-1] < lhs[-1]):
s_len += 1
return s_len - s_len % 2
t = int(input())
for _ in range(t):
s = input()
idxs = list()
max_len = -1
for _ in range(10):
idxs.append(list())
for idx, c in enumerate(s):
idxs[int(c)].append(idx)
for idx_arr in idxs:
max_len = max(max_len, len(idx_arr))
for i in range(10):
for j in range(i+1, 10):
lhs, rhs = idxs[i], idxs[j]
max_len = max(max_len, len_merged(lhs, rhs))
print(len(s) - max_len)
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define all(v) v.begin(),v.end()
#define pb push_back
void solve()
{
string s; cin >> s;
if (s.size() <= 2)
{
cout << 0 << "\n";
}
else
{
vector<ll> v[10];
for (ll i = 0; i < s.size(); ++i)
{
v[s[i] - '0'].pb(i);
}
ll i1 = -1, ans = LLONG_MAX;
for (ll i = 0; i <= 9; ++i)
{
if (v[i].size() > 0)
{
for (ll j = 0; j <= 9; ++j)
{
i1 = v[i][0];
ll temp = 0;
if (i == j) temp = 1;
if (v[j].size() == 0) continue;
while (1)
{
ll x = upper_bound(all(v[j]), i1) - v[j].begin();
if (x == v[j].size())
{
break;
}
else
{
if (i == j)
temp += 1;
else
temp += 2;
ll x1 = upper_bound(all(v[i]), v[j][x]) - v[i].begin();
if (x1 == v[i].size()) break;
if (i == j) temp++;
i1 = v[i][x1];
}
}
ans = min(ans, s.size() - temp);
}
}
}
cout << ans << "\n";
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
21. Merge Two Sorted Lists | 203. Remove Linked List Elements |
733. Flood Fill | 206. Reverse Linked List |
83. Remove Duplicates from Sorted List | 116. Populating Next Right Pointers in Each Node |
145. Binary Tree Postorder Traversal | 94. Binary Tree Inorder Traversal |
101. Symmetric Tree | 77. Combinations |
46. Permutations | 226. Invert Binary Tree |
112. Path Sum | 1556A - A Variety of Operations |
136. Single Number | 169. Majority Element |
119. Pascal's Triangle II | 409. Longest Palindrome |
1574A - Regular Bracket Sequences | 1574B - Combinatorics Homework |
1567A - Domino Disaster | 1593A - Elections |
1607A - Linear Keyboard | EQUALCOIN Equal Coins |
XOREQN Xor Equation | MAKEPAL Weird Palindrome Making |
HILLSEQ Hill Sequence | MAXBRIDGE Maximise the bridges |
WLDRPL Wildcard Replacement | 1221. Split a String in Balanced Strings |