|
| 1 | +# LeetCode 1347: Minimum Number of Steps to Make Two Strings Anagram |
| 2 | + |
| 3 | +## Problem Description |
| 4 | + |
| 5 | +Given two strings `s` and `t` of the same length, you want to change `t` in the minimum number of steps such that it becomes an anagram of `s`. A step consists of replacing one character in `t` with another character. |
| 6 | + |
| 7 | +An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. For example, "anagram" and "nagaram" are anagrams. |
| 8 | + |
| 9 | +Both strings consist of lowercase English letters. |
| 10 | + |
| 11 | +**Example 1:** |
| 12 | +Input: s = "bab", t = "aba" |
| 13 | +Output: 1 |
| 14 | +Explanation: Replace the first 'a' in t with b, t = "bba" which is an anagram of s. |
| 15 | + |
| 16 | +**Example 2:** |
| 17 | +Input: s = "leetcode", t = "practice" |
| 18 | +Output: 5 |
| 19 | +Explanation: Replace 'p', 'r', 'a', 'i', 'c' in t with 'l', 'e', 'e', 't', 'd' to form an anagram of s. |
| 20 | + |
| 21 | +**Example 3:** |
| 22 | +Input: s = "anagram", t = "mangaar" |
| 23 | +Output: 0 |
| 24 | +Explanation: "anagram" is already an anagram of "mangaar". |
| 25 | + |
| 26 | +## Solution in Go |
| 27 | + |
| 28 | +The core idea to solve this problem is to count the frequency of each character in both strings `s` and `t`. Since we want to transform `t` into an anagram of `s` by replacing characters in `t`, we need to identify characters in `t` that are "excess" compared to what `s` needs. |
| 29 | + |
| 30 | +For each character from 'a' to 'z': |
| 31 | +1. Count its occurrences in `s`. |
| 32 | +2. Count its occurrences in `t`. |
| 33 | +3. If the count of a character in `t` is greater than its count in `s`, it means `t` has `t_count - s_count` extra occurrences of this character. These extra occurrences must be replaced to match the character distribution of `s`. |
| 34 | +4. The sum of these differences for all characters will give us the minimum number of steps. |
| 35 | + |
| 36 | +This approach works because we only care about the characters that are *overrepresented* in `t`. Any characters that are *underrepresented* in `t` (i.e., `t_count < s_count`) will be formed by replacing the overrepresented characters. The total number of replacements needed is exactly the sum of the excesses. |
| 37 | + |
| 38 | +```go |
| 39 | +package main |
| 40 | + |
| 41 | +import "fmt" |
| 42 | + |
| 43 | +func minSteps(s string, t string) int { |
| 44 | + sFreq := make([]int, 26) // Frequency array for string s |
| 45 | + tFreq := make([]int, 26) // Frequency array for string t |
| 46 | + |
| 47 | + // Populate frequency array for string s |
| 48 | + for _, char := range s { |
| 49 | + sFreq[char-'a']++ |
| 50 | + } |
| 51 | + |
| 52 | + // Populate frequency array for string t |
| 53 | + for _, char := range t { |
| 54 | + tFreq[char-'a']++ |
| 55 | + } |
| 56 | + |
| 57 | + steps := 0 |
| 58 | + // Compare frequencies and calculate steps |
| 59 | + for i := 0; i < 26; i++ { |
| 60 | + // If character 'i' appears more times in t than in s, |
| 61 | + // these are the characters that need to be changed. |
| 62 | + if tFreq[i] > sFreq[i] { |
| 63 | + steps += tFreq[i] - sFreq[i] |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return steps |
| 68 | +} |
| 69 | + |
| 70 | +func main() { |
| 71 | + // Test cases |
| 72 | + fmt.Println(minSteps("bab", "aba")) // Expected: 1 |
| 73 | + fmt.Println(minSteps("leetcode", "practice")) // Expected: 5 |
| 74 | + fmt.Println(minSteps("anagram", "mangaar")) // Expected: 0 |
| 75 | + fmt.Println(minSteps("xxyyzz", "xxyyzz")) // Expected: 0 |
| 76 | + fmt.Println(minSteps("friend", "family")) // Expected: 4 |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +## Hashmap Solution |
| 81 | + |
| 82 | +```go |
| 83 | +package main |
| 84 | + |
| 85 | +import ( |
| 86 | + "fmt" |
| 87 | +) |
| 88 | + |
| 89 | +func minSteps(s string, t string) int { |
| 90 | + m := map[string]int{} |
| 91 | + for i := 0; i < len(s); i++ { |
| 92 | + m[string(s[i])]++ |
| 93 | + } |
| 94 | + for i := 0; i < len(t); i++ { |
| 95 | + m[string(t[i])]-- |
| 96 | + } |
| 97 | + steps := 0 |
| 98 | + for _, v := range m { |
| 99 | + steps += abs(v) |
| 100 | + } |
| 101 | + return steps / 2 |
| 102 | + |
| 103 | +} |
| 104 | + |
| 105 | +func abs(x int) int { |
| 106 | + if x < 0 { |
| 107 | + return -x |
| 108 | + } |
| 109 | + return x |
| 110 | +} |
| 111 | + |
| 112 | +func main() { |
| 113 | + fmt.Println(minSteps("bab", "aba")) |
| 114 | + fmt.Println(minSteps("leetcode", "practice")) |
| 115 | + fmt.Println(minSteps("anagram", "mangaar")) |
| 116 | + fmt.Println(minSteps("xxyyzz", "xxyyzz")) |
| 117 | + fmt.Println(minSteps("friend", "family")) |
| 118 | +} |
| 119 | + |
| 120 | +``` |
0 commit comments