Grokking the Engineering Manager Coding Interview

0% completed

Solution: Minimum Number of Steps to Make Two Strings Anagram

Problem Statement

Given two strings, s and t, of the same length, return the minimum number of steps required to make t an anagram of s.

In each step, you can replace any character in t with another character.

An anagram of a string is a string that contains the same characters in any order.

Examples

Example 1:

  • Input: s = "designgurus", t = "garumdespgn"
  • Expected Output: 3
  • Justification: We need to replace a, m, and p characters in the string t to match the frequency of characters in s.

Example 2:

.....

.....

.....

Like the course? Get enrolled and start learning!
Pavlo Iuriichuk

Pavlo Iuriichuk

· 9 days ago

Much easier (basically counter is a multi-set in this case):

from collections import Counter class Solution: def calculateSteps(self, s: str, t: str) -> int: freq_s = Counter(s) freq_t = Counter(t) return sum((freq_s - freq_t).values())