Prerequisite:
先决条件:
Hashing data structure
散列数据结构
Problem statement:
问题陈述:
Find the minimum number of steps to make two strings Anagram. Both strings are of the same length and the lower case. At each step, you can convert any character to string t to any other character.
找到使两个字符串Anagram最少的步骤数。 两个字符串的长度相同,并且都小写。 在每个步骤中,您都可以将字符串t中的任何字符转换为其他任何字符。
Example:
例:
Example 1:
String s= "bba"
String t= "aab"
Minimum number of steps to make two strings anagram: 1
String t can be converted to "bab"
which is anagram of string s="bba"
Example 2:
String s= "coding"
String t= "coders"
Minimum number of steps to make two strings anagram: 3
String t can be converted to "coding" which is anagram of
string s="coding"(basically here we need to convert into same string)
Solution:
解:
We can solve the problem by using hashing. Two strings are said to be an anagram of each other if both have the same set of characters with equal frequency.
我们可以通过使用哈希解决问题。 如果两个字符串都具有相同的频率相同的字符集,则称这两个字符串为彼此的字谜。
Now since both of the string is of the same length it's often possible to convert the string t to the string s. We can solve this by using a hashing and greedy algorithm.
Firstly we calculate two hash tables for both the strings s & t to store the frequencies for each character.
现在,由于两个字符串的长度相同,因此通常可以将字符串t转换为字符串s 。 我们可以通过使用哈希和贪婪算法来解决此问题。
首先,我们为字符串s & t计算两个哈希表,以存储每个字符的频率。
Let's say namely table1 and table2
比方说table1和table2
Both the table have 26 entries corresponding to 26 lowercase letters. The hash function that is used is: f(x)=x-'a' and instead of storing the characters itself we store the frequency like below:
两个表都有26个对应于26个小写字母的条目。 使用的哈希函数是:f(x)= x-'a',而不是存储字符本身,我们存储频率如下:
Set table1[26]={0} to store frequencies for string s
将table1 [26] = {0}设置为存储字符串s的频率
Set table2[26]={0} to store frequencies for string t
将table2 [26] = {0}设置为存储字符串t的频率
for(int i=0;i<s.length();i++) table1[s[i]-'a']++; table2[t[i]-'a']++; end for
Now after this both the table have 26 characters('a' to 'z') which have values 0(in case the character doesn't exist) or non-zero. Now, as the strings are of equal length it's guaranteed that string t can be converted so that it becomes an anagram of string s.
现在,在这之后,两个表都包含26个字符(“ a”至“ z”),其值均为0(以防字符不存在)或非零。 现在,由于字符串长度相等,因此可以保证可以将字符串t转换为字符串s的字谜。
So for each character(index in table) there can be three cases
因此,对于每个字符(表中的索引)可以有三种情况
For i =0 to 26
对于i = 0到26
table1[i]=table2[i]
table1 [i] = table2 [i]
Then no need to do anything as we need minimum no of conversion
然后,无需做任何事情,因为我们需要最少的转换次数
table2[i]>table1[i]
table2 [i]> table1 [i]
That means string
这意味着字符串
t has more number of same characters than in string s. Since in anagram both will the have same frequency for any character thus we need to convert the additional table2[i]-table1[i] to some other characters (not necessary to only one character)
t具有比字符串s中更多的相同字符。 由于在字谜中,两个字符的频率都相同,因此我们需要将附加的table2 [i] -table1 [i]转换为其他字符(不必仅转换为一个字符)
table2[i]<table1[i]
table2 [i] <table1 [i]
That means string
这意味着字符串
t has less number of same characters than in string s. Since in anagram both will the have same frequency for any character thus we have a deficiency of (table1[i]-table2[i]) number of characters which need to be converted from rest of the excess characters (which found in the second)
t的相同字符数少于字符串s中的相同字符数。 由于在字谜中,两个字符的频率都相同,因此我们缺少(table1 [i] -table2 [i])个字符,需要从其余多余字符(在第二个字符中找到)进行转换
So what is the minimum number of steps required?
那么,最少需要多少步?
It's basically
sum(table2[i]-table1[i]) if table2[i]-table1[i]
Set Steps=0
For i=0 to 25
If(table2[i]>table1[i])
Steps+= table2[i]-table1[i]
End for
Or
For i=0 to 25
If(table1[i]>table2[i])
Steps+= table1[i]-table2[i]
End for
Both algorithms will give the same result as both are anagrams of each other. But sticking to the question we are assuming that we are converting string t following the convention that if for any character the frequency in t is greater than s then we need to convert the extra characters which have deficiency string t.
两种算法的字谜结果都相同。 但是,坚持这个问题,我们假设要按照以下约定转换字符串t :如果对于任何字符, t中的频率大于s,则需要转换具有不足字符串t的多余字符。
#include <bits/stdc++.h>
using namespace std;
int minSteps(string s, string t)
{
int mymap1[26] = { 0 };
int mymap2[26] = { 0 };
for (int i = 0; i < s.length(); i++) {
mymap1[s[i] - 'a']++;
mymap2[t[i] - 'a']++;
}
int count = 0;
for (int i = 0; i < 26; i++) {
if (mymap2[i] > mymap1[i])
count += mymap2[i] - mymap1[i];
}
return count;
}
int main()
{
cout << "Enter string, s:\n";
string s;
cin >> s;
cout << "Enter string, t:\n";
string t;
cin >> t;
cout << "Minimum number of steps needed : " << minSteps(s, t) << endl;
return 0;
}
Output:
输出:
RUN 1:
Enter string, s:
abb
Enter string, t:
baa
Minimum number of steps needed : 1
RUN 2:
Enter string, s:
coders
Enter string, t:
coding
Minimum number of steps needed : 3
翻译自: https://www.includehelp.com/data-structure-tutorial/minimum-number-of-steps-to-make-two-strings-anagram.aspx