-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path1347.MinimumNumberofStepstoMakeTwoStringsAnagram.py
36 lines (32 loc) · 1.4 KB
/
1347.MinimumNumberofStepstoMakeTwoStringsAnagram.py
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
35
36
"""
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:
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.
Constraints:
- 1 <= s.length <= 50000
- s.length == t.length
- s and t contain lower-case English letters only.
"""
#Difficulty: Medium
#63 / 63 test cases passed.
#Runtime: 292 ms
#Memory Usage: 14.7 MB
#Runtime: 292 ms, faster than 32.39% of Python3 online submissions for Minimum Number of Steps to Make Two Strings Anagram.
#Memory Usage: 14.7 MB, less than 100.00% of Python3 online submissions for Minimum Number of Steps to Make Two Strings Anagram.
class Solution:
def minSteps(self, s: str, t: str) -> int:
s = ''.join(sorted(s))
t = ''.join(sorted(t))
intersection_letters = ''.join(list(set(s) & set(t)))
intersection_value = 0
if s != t:
for letter in intersection_letters:
intersection_value += min(s.count(letter), t.count(letter))
return len(s) - intersection_value if s != t else 0