Given two strings S and T of equal length. Both strings contain only the characters ‘0’ and ‘1’. The task is to find the minimum number of operations to convert string S to T. There are 2 types of operations allowed on string S:
- Swap any two characters of the string.
- Replace a ‘0’ with a ‘1’ or vice versa.
Input: S = “011”, T = “101”
Swap the first and second character.
Input: S = “010”, T = “101”
Swap the first and second character and replace the third character with ‘1’.
Approach: Find 2 values for the string S, the number of indices that have 0 but should be 1 and the number of indices that have 1 but should be 0. The result would be the maximum of these 2 values since we can use swaps on the minimum of these 2 values and the remaining unmatched characters can be inverted i.e. ‘0’ can be changed to ‘1’ and ‘1’ can be changed to ‘0’.
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 given operations required to convert a given binary string to all 1's
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum number of operations required to sum to binary string S
- Minimum number of operations required to obtain a given Binary 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 operations required to make the string satisfy the given condition
- Minimum swaps required to convert one binary string to another
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum reduce operations to convert a given string into a palindrome
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Minimum number of swaps required to make the string K periodic
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Minimum changes required to make first string substring of second string
- Minimum number of adjacent swaps to convert a string into its given anagram
- Convert string X to an anagram of string Y with minimum replacements
- Operations required to make the string empty
- Minimum number of operations required to make two strings equal
- Minimum number of given operations required to make two strings equal
- Minimum splits required to convert a number into prime segments
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.