array and hashmap December 4, 2022

Minimum number of steps to make two strings anagram

Time O(n) Space O(1) Open original problem

We will count the character frequency of both the strings. We will iterate over the character frequency of the first string and for each character we will find the number of characters that we need to remove from the second string to make the character frequency of the first string equal to the character frequency of the second string. We will keep track of the minimum number of characters that we need to remove from the second string and return it at the end.

class Solution:
    def minSteps(self, s: str, t: str) -> int:
        freqS, freqT = Counter(s), Counter(t)
        diff = freqT - freqS
        return sum(diff.values())

Time complexity: O(n)
Space complexity: O(1)