Leetcode 1469 Solution

This article provides solution to leetcode question 1469 (minimum-number-of-steps-to-make-two-strings-anagram)

https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram

Solution

class Solution:
    def minSteps(self, s: str, t: str) -> int:
        def get_ch_cnts(s):
            cnts = collections.defaultdict(int)
            for ch in s:
                cnts[ch] += 1
            return cnts

        s_cnts = get_ch_cnts(s)
        t_cnts = get_ch_cnts(t)

        ans = 0
        for i in range(26):
            ans += max(0, s_cnts[chr(i + ord('a'))] - t_cnts[chr(i + ord('a'))])

        return ans