Given two strings str1 and str2, the task is to count the minimum number of operations of the following three types on one of the two strings that are required to make str1 and str2 permutations of each other:
- Insert a character into the string.
- Remove a character from the string.
- Replace a character by another character from the string.
Note: All the above operations are of equal cost.
Input: str1 = “geeksforgeeks”, str2 = “geeksforcoder”
Explanation: Rearrange the string str2 to “geeksforcedor”
Replace the value of str1 to ‘c’.
Replace the value of str1 to ‘d’.
Replace the value of str1 to ‘o’.
Replace the value of str1 to ‘r’.
Therefore, the required output is 4.
Input: str1 = “geeks”, str2 = “keeg”
Approach: The problem can be solved using Hashing to store the frequency of each character of both the string. Below are the observations to solve the problem:
X = Number of characters which are present in both string, str1 and str2.
N1 – X = Number of characters present only in str1.
N2 – X = Number of characters present only in str2.
Total number of replacement operations = min(N1 – X, N2 – X)
Total number of insert/rRemove operations = max(N1 – X, N2 – X) – min(N1 – X, N2 – X).
Therefore, total number of operations = max(N1 – X, N2 – X),
Follow the steps below to solve the problem:
- Initialize two arrays, say freq1 and freq2 to store the frequency of all the characters of str1 and str2 respectively.
- Traverse both the strings and store the frequency of each character of both the strings in arrays freq1 and freq2 respectively.
- Traverse both the arrays freq1 and freq2.
- For every ith character, if freq1[i] exceeds freq2[i], then replace freq1[i] to freq1[i] – freq2[i] and set freq2[i] = 0 and vice-versa.
- Finally, calculate the sum of the arrays freq1 and freq2, and print the maximum between them as the answer
Below is the implement the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
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.
- Minimize replacements or swapping of same indexed characters required to make two given strings palindromic
- Minimize count of increments of each element of subarrays required to make Array non-increasing
- Check if two arrays are permutations of each other using Mathematical Operation
- Check if two Linked Lists are permutations of each other
- Check if two arrays are permutations of each other
- Check if given string can be formed by two other strings or their permutations
- Minimum number of given operations required to make two strings equal
- Minimum number of operations required to make two strings equal
- C Program to check if two given strings are isomorphic to each other
- Check if two given strings are isomorphic to each other
- Check if two strings are permutation of each other
- Check whether two strings are anagrams of each other using unordered_map in C++
- Check if two strings can be made equal by swapping one character among each other
- Check whether two strings are anagram of each other
- Count of cyclic permutations having XOR with other binary string as 0
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Minimum operations required to make two numbers equal
- Minimize operations required to obtain N
- Minimize steps required to make all array elements same by adding 1, 2 or 5
- Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
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.