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)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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 swaps required to make the string K periodic
- Minimum swaps required to move all vowels occurs after consonants in a given string
- 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 subsequences required to convert one string to another
- Minimum number of subsequences required to convert one string to another using Greedy Algorithm
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Minimum flips required to keep all 1s together in a Binary string
- Minimum jumps required to group all 1s together in a given Binary string
- Minimum number of operations required to sum to binary string S
- Minimum number of operations required to obtain a given Binary String
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Minimum size binary string required such that probability of deleting two 1's at random is 1/X