Subsequence Hate
Contest: Codeforces Round 646 (Div. 2)
Difficulty rating: 1400
Tags: implementation, strings

Shubham has a binary string $$$s$$$. A binary string is a string containing only characters "0" and "1".

He can perform the following operation on the string any amount of times:

- Select an index of the string, and flip the character at that index. This means, if the character was "0", it becomes "1", and vice versa.

A string is called good if it does not contain "010" or "101" as a subsequence  — for instance, "1001" contains "101" as a subsequence, hence it is not a good string, while "1000" doesn't contain neither "010" nor "101" as subsequences, so it is a good string.

What is the minimum number of operations he will have to perform, so that the string becomes good? It can be shown that with these operations we can make any string good.

A string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters.

Input format:
The first line of the input contains a single integer $$$t$$$ $$$(1\le t \le 100)$$$ — the number of test cases.

Each of the next $$$t$$$ lines contains a binary string $$$s$$$ $$$(1 \le |s| \le 1000)$$$.

Output format:
For every string, output the minimum number of operations required to make it good.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In test cases $$$1$$$, $$$2$$$, $$$5$$$, $$$6$$$ no operations are required since they are already good strings.

For the $$$3$$$rd test case: "001" can be achieved by flipping the first character  — and is one of the possible ways to get a good string.

For the $$$4$$$th test case: "000" can be achieved by flipping the second character  — and is one of the possible ways to get a good string.

For the $$$7$$$th test case: "000000" can be achieved by flipping the third and fourth characters  — and is one of the possible ways to get a good string.

Example input:
7
001
100
101
010
0
1
001100

Example output:
0
0
1
1
0
0
2

Editorial:
Key Idea: There are two types of good strings: Those which start with a series of $$$1$$$'s followed by $$$0$$$'s (such as $$$1111100$$$) and those which start with a series of $$$0$$$'s followed by $$$1$$$'s (such as $$$00111$$$). Note that there are strings which do belong to both categories (such as $$$000$$$).
Detailed Explanation: We will use the key idea to compute the minimum change required to achieve every possible string of each of the two types, and then take the minimum across them.
First, let us compute the total number of $$$1$$$'s and $$$0$$$'s in the string, denoted by num_ones and num_zeros. Now, as we iterate through the string, let us also maintain done_ones and done_zeros, which denote the number of $$$1$$$'s and $$$0$$$'s encountered so far.
Let us iterate through the string. When we are at position $$$i$$$ (indexed from $$$1$$$), we want to answer two questions: what is the cost for changing the string into $$$11..000$$$ (where number of $$$1$$$'s = $$$i$$$) and what is the cost for changing the string into $$$00..111$$$ (where number of $$$0$$$'s = $$$i$$$).
Assuming that done_zeros and done_ones also consider the current index, the answer to the first question is done_zeros + num_ones - done_ones. This is because done_zeros $$$0$$$'s must be converted to $$$1$$$'s, and num_ones - done_ones $$$1$$$'s must be converted to $$$0$$$'s. Similarly, the answer for the second question is done_ones + num_zeros - done_zeros.
The answer is the minimum over all such changes possible. Please do not forget to consider the all $$$1$$$'s and all $$$0$$$'s string in the above solution.
Time Complexity: $$$O(N)$$$