[LeetCode] 1347. Minimum Number of Steps to Make Two Strings Anagram
Updated:
1347. Minimum Number of Steps to Make Two Strings Anagram
t μ μλ λ¬Έμμ΄ μ€ μΌλΆλ₯Ό μμ ν΄μ s μ anagram ννλ‘ λ§λ€λ©΄ λλ€.
anagram μ μ΄λ€ λ¬Έμ₯μ λ¬Έμ μμλ₯Ό λ°κΏμ λ€λ₯Έ λ¬Έμ₯μΌλ‘ λμ΄μλ ννμ΄λ€.
μλ₯Όλ€μ΄ bcad λ abcd μ anagram μ΄λ€.
s = leetcode β {βeβ: 3, βlβ: 1, βtβ: 1, βcβ: 1, βoβ: 1, βdβ: 1}
t = practice β {βcβ: 2, βpβ: 1, βrβ: 1, βaβ: 1, βtβ: 1, βiβ: 1, βeβ: 1}
μ΄λ κ² λ¬Έμ λ³λ‘ κ°―μλ₯Ό ꡬν λ€ s - t λ₯Ό ν΄μ€λ€λ©΄ t μλ μκ³ s μλ§ μλ λ¬Έμ κ°μλ₯Ό ꡬν μ μλ€.
{βlβ: 1, βeβ: 2, βoβ: 1, βdβ: 1} t μμ λ¬Έμ 5κ°λ₯Ό μ΄λ κ² λ°κΏμ£Όλ©΄ t λ s μ anagram μ΄ λλ€.
Counter λ₯Ό ν΅ν΄ κ°―μλ₯Ό ꡬνκ³ λΊμ μ°μ°μ ν΅ν΄ κ°μ ꡬνλ©΄ λλ€.
λΊμ μ°μ°μ μ¬μ©νλ©΄ μμλ μ¬λΌμ§κΈ° λλ¬Έμ μ νν s μλ§ μλ λ¬Έμλ₯Ό ꡬν μ μλ€.
substarct μ μ¬μ©νλ©΄ μμ κ°λ μ λΆ λ°ννκ² λλ€.
from collections import Counter
class Solution:
def minSteps(self, s: str, t: str) -> int:
check = dict(Counter(s) - Counter(t))
output = 0
for val in check.values():
output += val
return output
test = Solution()
s = 'leetcode'
t = 'practice'
print(test.minSteps(s, t))
Leave a comment