Given two binary string M and N of equal length, the task is to find a minimum number of operations (swaps) required to convert string N to M.
Input: str1 = "1101", str2 = "1110" Output: 1 Swap last and second last element in the binary string, so that it become 1101 Input: str1 = "1110000", str2 = "0001101" Output: 3
Approach: Initialize the counter and Iterate over the M such that if any non-equal elements found in both binary strings, increment the counter. In the end, if the counter is even then print the result/2 because for one swap two elements are non-identical.
Suppose S1 = “10” and S2 = “01”, so two pairs are non-identical, the count = 2 and as the count is even, so number of swaps are count/2, i.e. 1. Even count determines that there are chances to swap the elements.
Below is the implementation of the above approach:
Time Complexity: O(n)
- Minimum swaps required to make a binary string alternating
- Minimum swaps required to make a binary string divisible by 2^k
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum given operations required to convert a given binary string to all 1's
- Minimum adjacent swaps required to Sort Binary array
- Minimum number of adjacent swaps to convert a string into its given anagram
- Minimum number of swaps to make two binary string equal
- Minimum number of given operations required to convert a string to another string
- Minimum number of operations required to sum to binary string S
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum steps to convert one binary string to other only using negation
- Minimum changes required to make first string substring of second string
- Minimum rotations required to get the same string
- Minimum rotations required to get the same String | Set-2
- Minimum changes required such that the string satisfies the given condition
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.