blob: cd3692ac31426f96a849341071982fef6bd34d5f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# 1347. Minimum Number of Steps to Make Two Strings Anagram
"""
given two trings of the same length 's' and 't'. in one step you can choose
any character of 't' and replace itwith another characters. return the
minimum numberof steps to make 't' an anagram of 's'. an anagram of a string
is a string that contains the same characters witha different ordering.
"""
class Solution(object):
def minSteps(self, s, t):
"""
:type s: str
:type t: str
:rtype: int
"""
cnt_s = [0] * 26
cnt_t = [0] * 26
for char in s:
cnt_s[ord(char) - ord("a")] += 1
for char in t:
cnt_t[ord(char) - ord("a")] += 1
steps = 0
for i in range(26):
steps += abs(cnt_s[i] - cnt_t[i])
return steps // 2
if __name__ == "__main__":
obj = Solution()
print(obj.minSteps(s="bab", t="aba"))
print(obj.minSteps(s="leetcode", t="practice"))
print(obj.minSteps(s="anagram", t="mangaar"))
|