Making a Good Binary String
Time limit2sMemory limit1024 MB
Given a binary string, find the fewest character flips so that the shortest intervals covering all 0s and all 1s have equal length.
- Level
Hard8 of 10
- Topics
- Prefix sum, Dynamic programming, Math
- Solved
- No attempts yet
Problem
A binary string of length made of and is given. For a cost of , you can change one character into the other character.
A good binary string is a string that satisfies all of the following conditions.
- It contains at least one and at least one .
- The minimum length of an interval containing all s is equal to the minimum length of an interval containing all s. An interval [, ] contains if every with satisfies (, , ). The length of an interval [, ] is defined as .
Find the minimum cost to turn the given string into a good binary string.
Input
The first line contains the number of test cases ().
Each test case consists of the following.
The first line contains the length of the binary string ().
The next line contains a string of length made of and .
It is guaranteed that the sum of over all test cases is at most .
Output
For each test case, print if the given string cannot be turned into a good binary string. Otherwise, print the minimum cost to turn it into a good binary string.