LeetCode 1864: Minimum Number of Swaps to Make the Binary String Alternating Solution
Master LeetCode problem 1864 (Minimum Number of Swaps to Make the Binary String Alternating), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
Problem Explanation
Explanation
To solve this problem, we need to check if it is possible to make the binary string alternating by swapping characters. We can do this by counting the number of zeros and ones in the string and then analyzing the possible cases. If the absolute difference between the counts of zeros and ones is at most 1, we can determine the minimum number of swaps required.
-
If the string length is odd, we have two cases:
- Case 1: The string starts with '0'. In this case, we need to count how many swaps are needed to make the string "0101...". This can be done by comparing the count of '0's at even indices and '1's at odd indices with the actual count.
- Case 2: The string starts with '1'. In this case, we need to count how many swaps are needed to make the string "1010...".
-
If the string length is even, we have only one case:
- We need to find the minimum swaps required to make the string "0101..." or "1010...".
If the minimum swaps required exceed half of the string length, it means it is impossible to make the string alternating.
Time Complexity: O(n), where n is the length of the input string. Space Complexity: O(1)
Solution Code
class Solution {
public int minSwaps(String s) {
int n = s.length();
int countZero = 0, countOne = 0;
for(char c : s.toCharArray()) {
if(c == '0') countZero++;
else countOne++;
}
if(Math.abs(countZero - countOne) > 1) return -1;
int minSwaps = 0;
if(n % 2 == 0) {
minSwaps = Math.min(countSwaps(s, '0'), countSwaps(s, '1'));
} else {
if(countZero > countOne) minSwaps = countSwaps(s, '0');
else minSwaps = countSwaps(s, '1');
}
return minSwaps / 2;
}
private int countSwaps(String s, char startChar) {
int swaps = 0;
for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) != startChar) swaps++;
startChar = startChar == '0' ? '1' : '0';
}
return swaps;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1864 (Minimum Number of Swaps to Make the Binary String Alternating)?
This page provides optimized solutions for LeetCode problem 1864 (Minimum Number of Swaps to Make the Binary String Alternating) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1864 (Minimum Number of Swaps to Make the Binary String Alternating)?
The time complexity for LeetCode 1864 (Minimum Number of Swaps to Make the Binary String Alternating) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1864 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1864 in Java, C++, or Python.