Minimum Number of Swaps to Make the Binary String Alternating - Complete Solution Guide
Minimum Number of Swaps to Make the Binary String Alternating is LeetCode problem 1864, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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: "1 1 10 0 0" -> "1 0 10 1 0" The string is now alternating. Example
Detailed Explanation
The problem requires finding the minimum number of swaps needed to transform a given binary string into an alternating binary string (e.g., "01010" or "10101"). An alternating string has no two adjacent characters that are the same. If it's impossible to create an alternating string from the given input (due to an imbalance in the number of '0's and '1's), the function should return -1. Swaps can be performed between any two characters in the string, regardless of their position.
Solution Approach
The solution first checks if forming an alternating string is possible by verifying that the absolute difference between the count of '0's and '1's is no more than 1. If it is possible, the solution proceeds based on whether the string length is odd or even. If odd, there is only one possible alternating pattern. If even, there are two possible alternating patterns, and the solution computes the swaps for each and returns the minimum. The core idea is that the minimum swaps required is the number of digits placed incorrectly in the 'even' indices of the candidate alternating pattern.
Step-by-Step Algorithm
- Step 1: Count the number of '1's and '0's in the given string.
- Step 2: Check if the absolute difference between the counts of '1's and '0's is greater than 1. If so, return -1 (impossible to form an alternating string).
- Step 3: If the length of the string is odd:
- Step 3a: Determine whether '1's or '0's are more frequent. The alternating string must start with the more frequent character.
- Step 3b: Iterate through the string, considering only characters at even indices (0, 2, 4, ...).
- Step 3c: Count the number of misplaced characters at even indices. If the target string starts with '1', count '0's. If the target string starts with '0', count '1's.
- Step 3d: Return the misplaced count as the minimum number of swaps.
- Step 4: If the length of the string is even:
- Step 4a: Calculate the number of swaps needed to make the string alternating starting with '0' (count misplaced '1's at even indices).
- Step 4b: Calculate the number of swaps needed to make the string alternating starting with '1' (count misplaced '0's at even indices).
- Step 4c: Return the minimum of the two swap counts calculated above.
Key Insights
- Insight 1: The number of '0's and '1's in the target alternating string can differ by at most 1. If the difference is greater than 1, an alternating string cannot be formed, and -1 should be returned.
- Insight 2: If the length of the string is odd, the alternating string is uniquely determined by whether '1' or '0' appears more frequently. The alternating string must start with the more frequent character.
- Insight 3: If the length of the string is even, there are two possible alternating strings: one starting with '0' and the other starting with '1'. We need to calculate the minimum swaps needed for both cases and return the smaller value.
- Insight 4: Counting the misplaced characters in one of the alternating string patterns gives the minimum number of swaps needed.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: String, Greedy.
Companies
Asked at: Societe Generale.