#K51517. Minimum Swap Operations
Minimum Swap Operations
Minimum Swap Operations
Given a binary string consisting only of the characters '0' and '1', your task is to determine the minimum number of swap operations needed to make all characters in the string identical. A swap operation is defined as swapping the positions of any two characters in the string.
If we let (n_0) be the count of '0's and (n_1) be the count of '1's in the string, then the answer is given by:
[
\text{operations} = \begin{cases}
0, & \text{if } n_0 = 0 \text{ or } n_1 = 0 \
1, & \text{otherwise}
\end{cases}
]
For example, the string 101010
requires exactly 1 swap operation to be converted into a uniform string. If the string is empty or all characters are already the same, no swaps are necessary.
inputFormat
The input is provided via standard input (stdin) and consists of a single line containing a binary string. This string may be empty and contains only the characters '0' and '1'.
outputFormat
Print a single integer to standard output (stdout), representing the minimum number of swap operations needed to transform the binary string into one where all characters are identical.## sample
0000
0