1821C.md (1741B)
1 Source: https://codeforces.com/problemset/problem/1821/C 2 3 C. Tear It Apart 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 You are given a string $s$, consisting of lowercase Latin letters. 22 23 In one operation, you can select several (one or more) positions in it such that no two selected positions are adjacent to each other. Then you remove the letters on the selected positions from the string. The resulting parts are concatenated without changing their order. 24 25 What is the smallest number of operations required to make all the letters in $s$ the same? 26 27 Input 28 29 The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases. 30 31 The only line of each testcase contains a string $s$, consisting of lowercase Latin letters. Its length is from $1$ to $2 \cdot 10^5$. 32 33 The total length of the strings over all testcases doesn't exceed $2 \cdot 10^5$. 34 35 Output 36 37 For each testcase, print a single integer — the smallest number of operations required to make all the letters in the given string $s$ the same. 38 39 Example 40 41 Input 42 43 ``` 44 5 45 46 abacaba 47 48 codeforces 49 50 oooooooo 51 52 abcdef 53 54 mewheniseearulhiiarul 55 ``` 56 57 Output 58 59 ``` 60 1 61 3 62 0 63 2 64 4 65 ``` 66 67 Note 68 69 In the first testcase, you can select positions $2, 4$ and $6$ and remove the corresponding letters 'b', 'c' and 'b'. 70 71 In the third testcase, the letters in the string are already the same, so you don't have to make any operations. 72 73 In the fourth testcase, one of the possible solutions in $2$ operations is the following. You can select positions $1, 4, 6$ first. The string becomes "bce". Then select positions $1$ and $3$. The string becomes "c". All letters in it are the same, since it's just one letter.