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 。 我们可以通过使用哈希和贪婪算法来解决此问题。
首先,我们为字符串st计算两个哈希表,以存储每个字符的频率。

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',而不是存储字符本身,我们存储频率如下:

  1. Set table1[26]={0} to store frequencies for string s

    将table1 [26] = {0}设置为存储字符串s的频率

  2. Set table2[26]={0} to store frequencies for string t

    将table2 [26] = {0}设置为存储字符串t的频率

  3. 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

  1. table1[i]=table2[i]

    table1 [i] = table2 [i]

    Then no need to do anything as we need minimum no of conversion

    然后,无需做任何事情,因为我们需要最少的转换次数

  2. 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]转换为其他字符(不必仅转换为一个字符)

  3. 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

制作两个字符串字谜的最小步骤数相关推荐

  1. LeetCode 1347. 制造字母异位词的最小步骤数

    1. 题目 给你两个长度相等的字符串 s 和 t.每一个步骤中,你可以选择将 t 中的 任一字符 替换为 另一个字符. 返回使 t 成为 s 的字母异位词的最小步骤数. 字母异位词 指字母相同,但排列 ...

  2. LeetCode刷题(107)~制造字母异位词的最小步骤数【巧妙】

    题目描述 给你两个长度相等的字符串 s 和 t.每一个步骤中,你可以选择将 t 中的 任一字符 替换为 另一个字符. 返回使 t 成为 s 的字母异位词的最小步骤数. 字母异位词 指字母相同,但排列不 ...

  3. 华为OD机试 - 最小步骤数(Python)

    最小步骤数 题目 一个正整数数组,设为nums 最大为100个成员 求从第一个成员开始正好走到数组最后一个成员所使用的最小步骤数 要求: 第一步,必须从第一元素起,且1 <= 第一步步长 < ...

  4. python字符串数组中最短的_python求解数组中两个字符串的最小距离

    题目: 给定一个数组 strs,其中的数据都是字符串,给定两个字符串 str1,str2.如果这两个字符串都在 strs数组中,就返回它们之间的最小距离:如果其中任何一个不在里面,则返回 -1:如果两 ...

  5. 字谜大全及答案100个_检查字谜(检查两个字符串是否是字谜)

    字谜大全及答案100个 Problem statement: Given two strings, check whether two given strings are anagram of eac ...

  6. leetcode712. 两个字符串的最小ASCII删除和(动态规划)-Gogo

    给定两个字符串s1, s2,找到使两个字符串相等所需删除字符的ASCII值的最小和. 示例 1: 输入: s1 = "sea", s2 = "eat" 输出: ...

  7. LeetCode 2186. 使两字符串互为字母异位词的最少步骤数

    文章目录 1. 题目 2. 解题 1. 题目 给你两个字符串 s 和 t .在一步操作中,你可以给 s 或者 t 追加 任一字符 . 返回使 s 和 t 互为 字母异位词 所需的最少步骤数. 字母异位 ...

  8. LeetCode 712. 两个字符串的最小ASCII删除和(DP,类似编辑距离)

    1. 题目 给定两个字符串s1, s2,找到使两个字符串相等所需删除字符的ASCII值的最小和. 示例 1: 输入: s1 = "sea", s2 = "eat" ...

  9. leetcode - 712. 两个字符串的最小ASCII删除和

    712. 两个字符串的最小ASCII删除和 -------------------------------------------- 给定两个字符串s1, s2,找到使两个字符串相等所需删除字符的AS ...

最新文章

  1. [笔记]Go语言在Linux环境下输出彩色字符
  2. python画小猪佩奇
  3. CentOS7(64位)安装Jupyter Notebook
  4. hystrix 配置 不生效_12、Feign整合断路器Hystrix
  5. Windows Server 2012正式版RDS系列⑥
  6. 总体经济拉动新引擎-农业大健康·张咏:疫情后谋定乡村振兴
  7. Elasticsearch2.x Breaking changes
  8. ML.NET 1.4 发布,跨平台机器学习框架
  9. python运算符有哪些_python中算数运算符都有哪些
  10. Apache Common-cli简单使用
  11. php图片写入带问号_php-fpm Remote Code Execution 分析(CVE-2019-11043)
  12. 浅谈java的静态代理模式
  13. iOS 用自签名证书实现 HTTPS 请求的原理
  14. DIV CSS设计时IE6、IE7、FF 与兼容性有关的特性
  15. 20分钟带你学会博弈论
  16. Txt格式的电子书解析
  17. 信息安全管理(CISP)—— 网络安全监管
  18. TFWmodi-修改tfw文件
  19. 肿瘤基因检测的解读流程
  20. 组合(Combination)

热门文章

  1. mysql客户端安装错误_windows下mysql 5.7以上版本安装及遇到的问题
  2. CSS3 iphone式开关的推荐写法
  3. 笔记《精通css》第2章 选择器,注释
  4. 「SDOI2014」数数 解题报告
  5. Leetcode 565. Array Nesting
  6. Codeforces 765F. Souvenirs
  7. BZOJ.1023.[SHOI2008]cactus仙人掌图(DP)
  8. link-cut-tree 简单介绍
  9. Java环境变量的设置
  10. Linux中vim编辑器的缩进的功能键