1864. Minimum Number of Swaps to Make the Binary String Alternating LeetCode Solution

In this guide, you will get 1864. Minimum Number of Swaps to Make the Binary String Alternating LeetCode Solution with the best time and space complexity. The solution to Minimum Number of Swaps to Make the Binary String Alternating problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Number of Swaps to Make the Binary String Alternating solution in C++
  4. Minimum Number of Swaps to Make the Binary String Alternating solution in Java
  5. Minimum Number of Swaps to Make the Binary String Alternating solution in Python
  6. Additional Resources
1864. Minimum Number of Swaps to Make the Binary String Alternating LeetCode Solution image

Problem Statement of Minimum Number of Swaps to Make the Binary String Alternating

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 <= 1000
s[i] is either '0' or '1'.

See also  3301. Maximize the Total Height of Unique Towers LeetCode Solution

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1864. Minimum Number of Swaps to Make the Binary String Alternating LeetCode Solution in C++

class Solution {
 public:
  int minSwaps(string s) {
    const int ones = ranges::count(s, '1');
    const int zeros = s.length() - ones;
    if (abs(ones - zeros) > 1)
      return -1;
    if (ones > zeros)
      return countSwaps(s, '1');
    if (zeros > ones)
      return countSwaps(s, '0');
    return min(countSwaps(s, '1'), countSwaps(s, '0'));
  }

 private:
  int countSwaps(const string& s, char curr) {
    int swaps = 0;
    for (const char c : s) {
      if (c != curr)
        ++swaps;
      curr ^= 1;
    }
    return swaps / 2;
  }
};
/* code provided by PROGIEZ */

1864. Minimum Number of Swaps to Make the Binary String Alternating LeetCode Solution in Java

class Solution {
  public int minSwaps(String s) {
    final int ones = (int) s.chars().filter(c -> c == '1').count();
    final int zeros = s.length() - ones;
    if (Math.abs(ones - zeros) > 1)
      return -1;
    if (ones > zeros)
      return countSwaps(s, '1');
    if (zeros > ones)
      return countSwaps(s, '0');
    return Math.min(countSwaps(s, '1'), countSwaps(s, '0'));
  }

  private int countSwaps(final String s, char curr) {
    int swaps = 0;
    for (final char c : s.toCharArray()) {
      if (c != curr)
        ++swaps;
      curr ^= 1;
    }
    return swaps / 2;
  }
}
// code provided by PROGIEZ

1864. Minimum Number of Swaps to Make the Binary String Alternating LeetCode Solution in Python

class Solution:
  def minSwaps(self, s: str) -> int:
    ones = s.count('1')
    zeros = len(s) - ones
    if abs(ones - zeros) > 1:
      return -1

    def countSwaps(curr: str) -> int:
      swaps = 0
      for c in s:
        if c != curr:
          swaps += 1
        curr = chr(ord(curr) ^ 1)
      return swaps // 2

    if ones > zeros:
      return countSwaps('1')
    if zeros > ones:
      return countSwaps('0')
    return min(countSwaps('1'), countSwaps('0'))
# code by PROGIEZ

Additional Resources

See also  122. Best Time to Buy and Sell Stock II LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.