Minimum number of steps to make two strings anagram II
December 4, 2022
array-and-hashmapProblem URL: Minimum number of steps to make two strings anagram II
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)
return (freqS-freqT).total() + (freqT-freqS).total()
Time complexity: O(n)
Space complexity: O(1)