📜  使两个字符串K 等价所需的最少插入或删除次数

📅  最后修改于: 2021-09-06 11:28:37             🧑  作者: Mango

1. 初始化两个数组，比如a1[]a2[] ，分别存储str1str2的每个不同字符的频率。
2. 迭代K 的所有可能值并跟踪ans 中的最小值：
• 迭代范围[0, 25]并检查以下条件：
• 如果str1 中不存在某个字符而str2 中存在某个字符，则应从str2 中删除所有这些字符。因此，应相应地更新计数
• 字符是否存在于STR1，然后更新通过在STR2该字符和K的当前值的频率的绝对差的计数乘以在STR1该字符的频率。
• count更新ans
3. 最后打印得到的count，即ans

##### C++
``````// C++ program for the above approach
#include
using namespace std;

// Function to find minimum count of operations
// required to map each character of str1 to K
// same characters of str2
int minOperations(string str1, string str2)
{

// Store frequency of each
// distinct character of str1
int a1 = { 0 };

// Store frequency of each
// distinct character of str2
int a2 = { 0 };

// Iterate over each character
// of the string, str1
for (auto x : str1) {

// Update frequency of
// current character
a1[x - 'a']++;
}

// Iterate over each character
// of the string, str2
for (auto x : str2) {

// Update frequency of
// current character
a2[x - 'a']++;
}

// Stores minimum count of operations
// required to map each character of
// str1 to K same characters of str2
int ans = INT_MAX;

// Iterate over all
// possible values of K
for (int k = 1; k < 1001; k++) {

// Stores count of operations required
// to map each character of str1 to k
// same characters of str2
int count = 0;

// Iterate over possible characters
for (int i = 0; i < 26; i++) {

// If (i + 'a') is not
// present in str1
if (a1[i] == 0) {

// Update count by removing
// character from str2
count += a2[i];
}

// If a character is
// present in str1
else {

// Update count by inserting
// character into str2
count += abs(a1[i] * k - a2[i]);
}
}

ans = min(ans, count);
}
return ans;
}

// Driver Code
int main()
{
string str1 = "aaa";
string str2 = "bbb";

// Function Call
cout << minOperations(str1, str2) << endl;

return 0;
}``````

##### Java
``````// Java program for the above approach
class GFG
{
// Function to find minimum count of operations
// required to map each character of str1 to K
// same characters of str2
static int minOperations(String str1, String str2)
{

// Store frequency of each
// distinct character of str1
int[] a1 = new int;

// Store frequency of each
// distinct character of str2
int[] a2 = new int;

// Iterate over each character
// of the string, str1
for (int x = 0; x < str1.length(); x++)
{

// Update frequency of
// current character
a1[str1.charAt(x) - 'a']++;
}

// Iterate over each character
// of the string, str2
for (int x = 0; x < str2.length(); x++)
{

// Update frequency of
// current character
a2[str2.charAt(x) - 'a']++;
}

// Stores minimum count of operations
// required to map each character of
// str1 to K same characters of str2
int ans = Integer.MAX_VALUE;

// Iterate over all
// possible values of K
for (int k = 1; k < 1001; k++)
{

// Stores count of operations required
// to map each character of str1 to k
// same characters of str2
int count = 0;

// Iterate over possible characters
for (int i = 0; i < 26; i++)
{

// If (i + 'a') is not
// present in str1
if (a1[i] == 0)
{

// Update count by removing
// character from str2
count += a2[i];
}

// If a character is
// present in str1
else
{

// Update count by inserting
// character into str2
count += Math.abs(a1[i] * k - a2[i]);
}
}

ans = Math.min(ans, count);
}
return ans;
}

// Driver code
public static void main(String[] args)
{
String str1 = "aaa";
String str2 = "bbb";

// Function Call
System.out.println(minOperations(str1, str2));
}
}

// This code is contributed by divyeshrabadiya07``````

##### Python3
``````# Python program for the above approach

# Function to find minimum count of operations
# required to map each character of str1 to K
# same characters of str2
import sys
def minOperations(str1, str2):

# Store frequency of each
# distinct character of str1
a1 =  * 26;

# Store frequency of each
# distinct character of str2
a2 =  * 26;

# Iterate over each character
# of the string, str1
for x in range(len(str1)):

# Update frequency of
# current character
a1[ord(str1[x]) - ord('a')] += 1;

# Iterate over each character
# of the string, str2
for x in range(len(str2)):

# Update frequency of
# current character
a2[ord(str2[x]) - ord('a')] += 1;

# Stores minimum count of operations
# required to map each character of
# str1 to K same characters of str2
ans = sys.maxsize;

# Iterate over all
# possible values of K
for k in range(1, 1001):

# Stores count of operations required
# to map each character of str1 to k
# same characters of str2
count = 0;

# Iterate over possible characters
for i in range(26):

# If (i + 'a') is not
# present in str1
if (a1[i] == 0):

# Update count by removing
# character from str2
count += a2[i];

# If a character is
# present in str1
else:

# Update count by inserting
# character into str2
count += abs(a1[i] * k - a2[i]);

ans = min(ans, count);
return ans;

# Driver code
if __name__ == '__main__':
str1 = "aaa";
str2 = "bbb";

# Function Call
print(minOperations(str1, str2));

# This code is contributed by 29AjayKumar``````

##### C#
``````// C# program for the above approach
using System;

class GFG{

// Function to find minimum count of operations
// required to map each character of str1 to K
// same characters of str2
static int minOperations(string str1, string str2)
{

// Store frequency of each
// distinct character of str1
int[] a1 = new int;

// Store frequency of each
// distinct character of str2
int[] a2 = new int;

// Iterate over each character
// of the string, str1
foreach(char x in str1)
{

// Update frequency of
// current character
a1[x - 'a']++;
}

// Iterate over each character
// of the string, str2
foreach(char x in str2)
{

// Update frequency of
// current character
a2[x - 'a']++;
}

// Stores minimum count of operations
// required to map each character of
// str1 to K same characters of str2
int ans = Int32.MaxValue;

// Iterate over all
// possible values of K
for(int k = 1; k < 1001; k++)
{

// Stores count of operations required
// to map each character of str1 to k
// same characters of str2
int count = 0;

// Iterate over possible characters
for(int i = 0; i < 26; i++)
{

// If (i + 'a') is not
// present in str1
if (a1[i] == 0)
{

// Update count by removing
// character from str2
count += a2[i];
}

// If a character is
// present in str1
else
{

// Update count by inserting
// character into str2
count += Math.Abs(a1[i] * k - a2[i]);
}
}

ans = Math.Min(ans, count);
}
return ans;
}

// Driver code
static void Main()
{
string str1 = "aaa";
string str2 = "bbb";

// Function Call
Console.WriteLine(minOperations(str1, str2));
}
}

// This code is contributed by divyesh072019``````

##### Javascript
``````

// Javascript program for the above approach

// Function to find minimum count of operations
// required to map each character of str1 to K
// same characters of str2
function minOperations(str1, str2)
{

// Store frequency of each
// distinct character of str1
let a1 = new Array(26);
a1.fill(0);

// Store frequency of each
// distinct character of str2
let a2 = new Array(26);
a2.fill(0);

// Iterate over each character
// of the string, str1
for(let x = 0; x < str1.length; x++)
{

// Update frequency of
// current character
a1[str1[x].charCodeAt() - 'a'.charCodeAt()]++;
}

// Iterate over each character
// of the string, str2
for(let x = 0; x < str2.length; x++)
{

// Update frequency of
// current character
a2[str2[x].charCodeAt() - 'a'.charCodeAt()]++;
}

// Stores minimum count of operations
// required to map each character of
// str1 to K same characters of str2
let ans = Number.MAX_VALUE;

// Iterate over all
// possible values of K
for(let k = 1; k < 1001; k++)
{

// Stores count of operations required
// to map each character of str1 to k
// same characters of str2
let count = 0;

// Iterate over possible characters
for(let i = 0; i < 26; i++)
{

// If (i + 'a') is not
// present in str1
if (a1[i] == 0)
{

// Update count by removing
// character from str2
count += a2[i];
}

// If a character is
// present in str1
else
{

// Update count by inserting
// character into str2
count += Math.abs(a1[i] * k - a2[i]);
}
}

ans = Math.min(ans, count);
}
return ans;
}

let str1 = "aaa";
let str2 = "bbb";

// Function Call
document.write(minOperations(str1, str2));

``````

``6``