Given two strings of equal lengths str1 and str2 consisting of characters ‘a’ and ‘b’ only. The following operations can be performed on str1:
- Any character can be changed from ‘a’ to ‘b’ or from ‘b’ to ‘a’ with 1 unit cost.
- Any two characters str1[i] and str1[j] can be swapped with cost |i – j|.
The task is to find the minimum cost required to convert str1 to str2.
Input: str1 = “abb”, str2 = “baa”
Swap(str1, str1), str1 = “bab” and cost = 1
Change str1 = ‘b’ to ‘a’, str1 = “baa” and cost = 2
Input: str1 = “abab”, str2 = “aabb”
Approach: It can be observed that swapping will only be performed on consecutive characters because if the characters are not consecutive then the cost of swapping will be ≥ 2 which will give cost greater than or equal to the cost of changing those characters using the operation of the first type. Now, for every two consecutive characters if they are different in both the string then swap these characters incurring 1 unit cost as compares to 2 unit cost when both of them are changed separately. Else change the character which is different in both the strings (either the current or the next). Finally, print the calculated cost.
Below is the implementation of the above approach:
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.
- Count of characters in str1 such that after deleting anyone of them str1 becomes str2
- Check whether str1 can be converted to str2 with the given operations
- Generate all possible strings such that char at index i is either str1[i] or str2[i]
- Largest substring of str2 which is a prefix of str1
- Maximum number of times str1 appears as a non-overlapping substring in str2
- Transform string str1 into str2 by taking characters from string str3
- Minimum cost to convert one given string to another using swap, insert or delete operations
- Minimize cost to convert given two integers to zero using given operations
- Minimum cost to empty Array where cost of removing an element is 2^(removed_count) * arr[i]
- Minimum cost to convert given string to consist of only vowels
- Minimum given operations required to convert a given binary string to all 1's
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Minimum cost to sort strings using reversal operations of different costs
- Minimum cost to reach a point N from 0 with two different operations allowed
- Minimum cost to convert string into palindrome
- Minimum cost required to convert all Subarrays of size K to a single element
- Minimum cost of flipping characters required to convert Binary String to 0s only
- Minimum number of operations to convert a given sequence into a Geometric Progression
- Minimum number of given operations required to convert a string to another string
- Minimum operations required to convert all characters of a String to a given 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.