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

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

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。导致错误。

相关文章
|
4月前
|
Go
golang力扣leetcode 49.字母异位词分组
golang力扣leetcode 49.字母异位词分组
29 0
|
4月前
|
Go
golang力扣leetcode 438.找到字符串中所有字母异位词
golang力扣leetcode 438.找到字符串中所有字母异位词
30 0
|
10月前
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
89 1
|
3月前
|
算法 容器
【LeetCode刷题】滑动窗口解决问题:水果成篮、找到字符串中所有字母异位词
【LeetCode刷题】滑动窗口解决问题:水果成篮、找到字符串中所有字母异位词
|
3月前
|
存储 SQL 算法
LeetCode 题目 65:有效数字(Valid Number)【python】
LeetCode 题目 65:有效数字(Valid Number)【python】
|
3月前
|
存储 算法 安全
LeetCode 题目 49:字母异位词分组 5种算法实现与典型应用案例【python】
LeetCode 题目 49:字母异位词分组 5种算法实现与典型应用案例【python】
|
3月前
|
存储
力扣经典150题第四十二题:字母异位词分组
力扣经典150题第四十二题:字母异位词分组
19 0
|
3月前
|
存储
力扣经典150题第四十一题:有效的字母异位词
力扣经典150题第四十一题:有效的字母异位词
18 0
|
3月前
|
算法
【经典LeetCode算法题目专栏分类】【第8期】滑动窗口:最小覆盖子串、字符串排列、找所有字母异位词、 最长无重复子串
【经典LeetCode算法题目专栏分类】【第8期】滑动窗口:最小覆盖子串、字符串排列、找所有字母异位词、 最长无重复子串
|
4月前
leetcode代码记录(有效的字母异位词
leetcode代码记录(有效的字母异位词
26 1