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 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 swaps required to make a binary string alternating
- Minimum swaps required to make a binary string divisible by 2^k
- Minimum number of given operations required to convert a string to another string
- 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
- Check if it is possible to convert one string into another with given constraints
- Minimum number of swaps to make two binary string equal
- Number of steps required to convert a binary number to one
- 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
- Find lexicographically smallest string in at most one swaps
- Minimum steps to convert one binary string to other only using negation
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Transform One String to Another using Minimum Number of Given Operation
- Minimum cost to traverse from one index to another in the String
- Convert given string to another by minimum replacements of subsequences by its smallest character
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.