1864. Minimum Number of Swaps to Make the Binary String Alternating
SourceWeekly Contest 241 Q2DifficultyMediumRating1600
Description
Given a binary string s, return the minimum number of character swaps to make it alternating, or -1 if it is impossible.
The string is called alternating if no two adjacent characters are equal. For example, the strings "010" and "1010" are alternating, while the string "0100" is not.
Any two characters may be swapped, even if they are not adjacent.
Example 1:
Input: s = "111000" Output: 1 Explanation: Swap positions 1 and 4: "111000" -> "101010" The string is now alternating.
Example 2:
Input: s = "010" Output: 0 Explanation: The string is already alternating, no swaps are needed.
Example 3:
Input: s = "1110" Output: -1
Constraints:
1 <= s.length <= 1000s[i]is either'0'or'1'.
Solutions
Solution 1: Counting
Thinking
We may swap bits to reach \(0101\ldots\) or \(1010\ldots\). The two targets demand different \(0/1\) counts, so a mismatch of more than one is impossible.
Compare \(n_0\) and \(n_1\): if they differ by more than \(1\), return \(-1\); if equal, try both starting bits; otherwise start with the majority bit. Each swap fixes two mismatches, so the answer is half the number of wrong positions.
First, we count the number of characters \(0\) and \(1\) in the string \(\textit{s}\), denoted as \(n_0\) and \(n_1\) respectively.
If the absolute difference between \(n_0\) and \(n_1\) is greater than \(1\), it is impossible to form an alternating string, so we return \(-1\).
If \(n_0\) and \(n_1\) are equal, we can calculate the number of swaps needed to convert the string into an alternating string starting with \(0\) and starting with \(1\), and take the minimum value.
If \(n_0\) and \(n_1\) are not equal, we only need to calculate the number of swaps needed to convert the string into an alternating string starting with the character that appears more frequently.
The problem is reduced to calculating the number of swaps needed to convert the string \(\textit{s}\) into an alternating string starting with character \(c\).
We define a function \(\text{calc}(c)\), which represents the number of swaps needed to convert the string \(\textit{s}\) into an alternating string starting with character \(c\). We traverse the string \(\textit{s}\), and for each position \(i\), if the parity of \(i\) is different from \(c\), we need to swap the character at this position, incrementing the counter by \(1\). Since each swap makes two positions have the same character, the final number of swaps is half of the counter.
The time complexity is \(O(n)\), where \(n\) is the length of the string \(\textit{s}\). The space complexity is \(O(1)\).
1 2 3 4 5 6 7 8 9 10 11 12 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |