[leetcode] 1347. Minimum Number of Steps to Make Two Strings Anagram

网友投稿 742 2022-10-01

[leetcode] 1347. Minimum Number of Steps to Make Two Strings Anagram

[leetcode] 1347. Minimum Number of Steps to Make Two Strings Anagram

Description

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: 1Explanation: Replace the first 'a' in t with b, t = "bba" which is anagram of s.

Example 2:

Input: s = "leetcode", t = "practice"Output: 5Explanation: 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: 0Explanation: "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 <= 50000s.length == t.lengths and t contain lower-case English letters only.

分析

题目的意思是:给定两个字符串,可以替换其中一个字符串的字符,问最小的替换数使得两字符串相等,允许字符串的位置不一样。我的思路很直接,直接统计第一个字符串s的字符频率,然后再去比较第二个字符串t,然后找出字典中不存在的字符的个数就是结果了。这道题有点简单,我有点紧张。

代码

class Solution: def minSteps(self, s: str, t: str) -> int: d=collections.defaultdict(int) for ch in s: d[ch]+=1 res=0 for ch in t: if(ch in d and d[ch]>0): d[ch]-=1 else: res+=1 return res

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:[leetcode] 1311. Get Watched Videos by Your Friends
下一篇:微信小程序不能用怎么办(微信里小程序不能用怎么回事)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~