Minimum insertions or deletions required to make two strings K-equivalent
Last Updated :
30 Sep, 2022
Given two strings str1 and str2, the task is to find the minimum number of operations required to map each character of the string str1 to K ( < 1000) similar characters of the string str2 by either inserting a character into str2 or by removing a character from str2.
Examples:
Input: str1 = “aab”, str2 = “aaaabb”
Output: 0
Explanation:
Map {str2[0], str2[1]} to str1[0]
Map {str2[2], str2[3]} to str1[1].
Map {str2[4], str2[5]} to str1[2]
Since no operations are required to map each character of str1 to K(= 2) similar characters of str2.
Therefore, the required output is 0.
Input: str1 = “aaa”, str2 = “bbb”
Output: 6
Explanation:
Removing str2[0], str2[1], str2[2] and inserting “aaa” modifies str2 to “aaa”.
Map { str2[0] } to str1[0], str2[1] to str1[1] and {str2[2]} to str1[2].
Therefore, the required output is 6.
Approach: To solve this problem, select the best value of K such that the number of operations required is minimum. To select the optimal value of K, iterate over all possible values of K and keep a track of the count of operations required for each of them. Finally, print the minimum count obtained. Follow the steps below to solve the problem:
- Initialize two arrays, say a1[] and a2[], to store the frequency of each distinct character of str1 and str2 respectively.
- Iterate over all possible values of K and keep a track of minimum till now in ans:
- Iterate over the range [0, 25] and check the following conditions:
- If a character is not present in str1 and present in str2, then all these characters should be removed from str2. Hence, the count should be updated accordingly.
- If a character is present in str1, then update the count by the absolute difference of frequency of that character in str2 and the current value of K multiplied by the frequency of that character in str1.
- Update the ans with the count.
- Finally, print the count obtained, i.e. ans
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperations(string str1, string str2)
{
int a1[26] = { 0 };
int a2[26] = { 0 };
for ( auto x : str1) {
a1[x - 'a' ]++;
}
for ( auto x : str2) {
a2[x - 'a' ]++;
}
int ans = INT_MAX;
for ( int k = 1; k < 1001; k++) {
int count = 0;
for ( int i = 0; i < 26; i++) {
if (a1[i] == 0) {
count += a2[i];
}
else {
count += abs (a1[i] * k - a2[i]);
}
}
ans = min(ans, count);
}
return ans;
}
int main()
{
string str1 = "aaa" ;
string str2 = "bbb" ;
cout << minOperations(str1, str2) << endl;
return 0;
}
|
Java
class GFG
{
static int minOperations(String str1, String str2)
{
int [] a1 = new int [ 26 ];
int [] a2 = new int [ 26 ];
for ( int x = 0 ; x < str1.length(); x++)
{
a1[str1.charAt(x) - 'a' ]++;
}
for ( int x = 0 ; x < str2.length(); x++)
{
a2[str2.charAt(x) - 'a' ]++;
}
int ans = Integer.MAX_VALUE;
for ( int k = 1 ; k < 1001 ; k++)
{
int count = 0 ;
for ( int i = 0 ; i < 26 ; i++)
{
if (a1[i] == 0 )
{
count += a2[i];
}
else
{
count += Math.abs(a1[i] * k - a2[i]);
}
}
ans = Math.min(ans, count);
}
return ans;
}
public static void main(String[] args)
{
String str1 = "aaa" ;
String str2 = "bbb" ;
System.out.println(minOperations(str1, str2));
}
}
|
Python3
import sys
def minOperations(str1, str2):
a1 = [ 0 ] * 26 ;
a2 = [ 0 ] * 26 ;
for x in range ( len (str1)):
a1[ ord (str1[x]) - ord ( 'a' )] + = 1 ;
for x in range ( len (str2)):
a2[ ord (str2[x]) - ord ( 'a' )] + = 1 ;
ans = sys.maxsize;
for k in range ( 1 , 1001 ):
count = 0 ;
for i in range ( 26 ):
if (a1[i] = = 0 ):
count + = a2[i];
else :
count + = abs (a1[i] * k - a2[i]);
ans = min (ans, count);
return ans;
if __name__ = = '__main__' :
str1 = "aaa" ;
str2 = "bbb" ;
print (minOperations(str1, str2));
|
C#
using System;
class GFG{
static int minOperations( string str1, string str2)
{
int [] a1 = new int [26];
int [] a2 = new int [26];
foreach ( char x in str1)
{
a1[x - 'a' ]++;
}
foreach ( char x in str2)
{
a2[x - 'a' ]++;
}
int ans = Int32.MaxValue;
for ( int k = 1; k < 1001; k++)
{
int count = 0;
for ( int i = 0; i < 26; i++)
{
if (a1[i] == 0)
{
count += a2[i];
}
else
{
count += Math.Abs(a1[i] * k - a2[i]);
}
}
ans = Math.Min(ans, count);
}
return ans;
}
static void Main()
{
string str1 = "aaa" ;
string str2 = "bbb" ;
Console.WriteLine(minOperations(str1, str2));
}
}
|
Javascript
<script>
function minOperations(str1, str2)
{
let a1 = new Array(26);
a1.fill(0);
let a2 = new Array(26);
a2.fill(0);
for (let x = 0; x < str1.length; x++)
{
a1[str1[x].charCodeAt() - 'a' .charCodeAt()]++;
}
for (let x = 0; x < str2.length; x++)
{
a2[str2[x].charCodeAt() - 'a' .charCodeAt()]++;
}
let ans = Number.MAX_VALUE;
for (let k = 1; k < 1001; k++)
{
let count = 0;
for (let i = 0; i < 26; i++)
{
if (a1[i] == 0)
{
count += a2[i];
}
else
{
count += Math.abs(a1[i] * k - a2[i]);
}
}
ans = Math.min(ans, count);
}
return ans;
}
let str1 = "aaa" ;
let str2 = "bbb" ;
document.write(minOperations(str1, str2));
</script>
|
Time Complexity: O(N + 26 * K)
Auxiliary Space: O(1) as constant size array a1 and a2 are used.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...