网站开发整合编辑器,wordpress 百度翻译,网络营销与直播电商专升本,网站建设的投资预算怎么写题目要求
给你两个长度相同的字符串 s 和 t。在一个步骤中#xff0c;你可以选择 t 中的任意一个字符并用另一个字符替换它。
返回将 t 变为 s 的变位所需的最少步数。
字符串的 字谜 是指字符串中的相同字符具有不同#xff08;或相同#xff09;的排列顺序…题目要求
给你两个长度相同的字符串 s 和 t。在一个步骤中你可以选择 t 中的任意一个字符并用另一个字符替换它。
返回将 t 变为 s 的变位所需的最少步数。
字符串的 字谜 是指字符串中的相同字符具有不同或相同的排列顺序。
Example 1:
Input: s bab, t aba
Output: 1
Explanation: Replace the first a in t with b, t bba which is anagram of s.Example 2:
Input: s leetcode, t practice
Output: 5
Explanation: Replace p, r, a, i and c from t with proper characters to make t anagram of s.Example 3:
Input: s anagram, t mangaar
Output: 0
Explanation: anagram and mangaar are anagrams.
思路
看起来非常的简单用哈希表统计每个字符出现的次数如果字母a在单词bbb中没出现过那么次数即为零。统计两个字符串相同字母出现次数的差值相加除以2就是我们所需要的答案。 代码
class Solution {
public:int minSteps(string s, string t) {int ans 0;unordered_mapchar, int map1, map2;for (int i 0; i s.size(); i) {map1[s[i]];map2[t[i]];}for (auto p : map1) {ans abs(p.second - map2[p.first]);}for (auto p : map2) {if (map1.find(p.first) map1.end()) {ans p.second;}}return int(ans/2);}
};
时空复杂度