LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram

Table of Contents

中文版:

英文版:

My answer:

解题报告:


中文版:

给你两个长度相等的字符串 s 和 t。每一个步骤中,你可以选择将 t 中的 任一字符 替换为 另一个字符。

返回使 t 成为 s 的字母异位词的最小步骤数。

字母异位词 指字母相同,但排列不同的字符串。

示例 1:

输出:s = "bab", t = "aba"
输出:1
提示:用 'b' 替换 t 中的第一个 'a',t = "bba" 是 s 的一个字母异位词。
示例 2:

输出:s = "leetcode", t = "practice"
输出:5
提示:用合适的字符替换 t 中的 'p', 'r', 'a', 'i' 和 'c',使 t 变成 s 的字母异位词。
示例 3:

输出:s = "anagram", t = "mangaar"
输出:0
提示:"anagram" 和 "mangaar" 本身就是一组字母异位词。 
示例 4:

输出:s = "xxyyzz", t = "xxyyzz"
输出:0
示例 5:

输出:s = "friend", t = "family"
输出:4
 

提示:

1 <= s.length <= 50000
s.length == t.length
s 和 t 只包含小写英文字母

英文版:

Given two equal-size strings s and t. In one step you can choose any character of t and replace it with another character.

Return the minimum number of steps to make t an anagram of s.

An Anagram of a string is a string that contains the same characters with a different (or the same) ordering.

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. 
Example 4:

Input: s = "xxyyzz", t = "xxyyzz"
Output: 0
Example 5:

Input: s = "friend", t = "family"
Output: 4
 

Constraints:

1 <= s.length <= 50000
s.length == t.length
s and t contain lower-case English letters only.

My answer:

class Solution:
    def minSteps(self, s: str, t: str) -> int:
        s_dict = {}
        t_dict = {}
        for i in range(len(s)):
            if s[i] not in s_dict.keys():
                s_dict[s[i]] = 1
            else:
                s_dict[s[i]] += 1
        for i in range(len(t)):
            if t[i] not in t_dict.keys():
                t_dict[t[i]] = 1
            else:
                t_dict[t[i]] += 1
        # print(s_dict)
        # print(t_dict)
        res = 0
        for key in s_dict.keys():
            # print(key)
            if (key in t_dict.keys()) :
                if (s_dict[key] - t_dict[key] > 0):
                    num = s_dict[key] - t_dict[key]
                # print(num)
                    res += num
            else:
                res += s_dict[key]
        return res

代码中的 print 都是为了调 bug。万能 print 大法,就能直接看到哪里和自己预想的不一样。

解题报告:

算法:将 s , t 的内容存在两个 dict 中,其中 key 是 字符串中出现的字符,value 是该字符出现的个数。之后两个 dict 比较,遍历 s_dict 中的每个可以,如果 t_dict 存在该 key,且 s 中个数 > t,则证明需要改变 s-t 个,所以结果中加上 s-t 。注意不用考虑 s< t的情况,因为s 和 t 长度相同,如果 s和t 中字母也相同,则 s > t 时把 t 少的字母变成 s 中的字母即可,比如 bab 和 aba,只要将 t 中的 a 变为 b 即可。注意找好基准,本代码以 s 为基准。 例子 2 中 leetcode 比 practice 多 2 个 e,和 l c d。 

解答本题的时候犯了个错误,在 for 循环里面的两个 if 处,一开始的写法是

if (key in t_dict.keys())  and if (s_dict[key] - t_dict[key] > 0):

然后下面的 else的意思就是 不满足 and 前的条件,或不满足 and 后的条件,而我想要的是不满足and 前的条件就进入 else。导致错误。

发布了112 篇原创文章 · 获赞 41 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/u011675334/article/details/104238196