[Solution] Binary String solution codeforces

Binary String solution codeforces – You are given a string đť‘ s consisting of characters 0 and/or 1.

[Solution] Binary String solution codeforces

You have to remove several (possibly zero) characters from the beginning of the string, and then several (possibly zero) characters from the end of the string. The string may become empty after the removals. The cost of the removal is the maximum of the following two values:

  • the number of characters 0 left in the string;
  • the number of characters 1 removed from the string.

What is the minimum cost of removal you can achieve?

[Solution] Binary String solution codeforces

The first line contains one integer 𝑡t (1≤𝑡≤1041≤t≤104) — the number of test cases.

Each test case consists of one line containing the string 𝑠s (1≤|𝑠|≤2⋅1051≤|s|≤2⋅105), consisting of characters 0 and/or 1.

The total length of strings 𝑠s in all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, print one integer — the minimum cost of removal you can achieve.

Example
input

Copy

[Solution] Binary String solution codeforces

5
101110110
1001001001001
0000111111
00000
1111
output

Copy
1
3
0
0
0

Binary String solution codeforces

Consider the test cases of the example:

  1. in the first test case, it’s possible to remove two characters from the beginning and one character from the end. Only one 1 is deleted, only one 0 remains, so the cost is 11;
  2. in the second test case, it’s possible to remove three characters from the beginning and six characters from the end. Two characters 0 remain, three characters 1 are deleted, so the cost is 33;
  3. in the third test case, it’s optimal to remove four characters from the beginning;
  4. in the fourth test case, it’s optimal to remove the whole string;
  5. in the fifth test case, it’s optimal to leave the string as it is.

For Solution

Click here

Leave a Comment