You are given two strings of the same length s and t. In one step you can choose any character of t and replace it with another character.
Return the minimum number of steps to make t an anagram of s.
An Anagram of a string is a string that contains the same characters with a different (or the same) ordering.
Example 1:
Input: s = "bab", t = "aba" Output: 1 Explanation: Replace the first 'a' in t with b, t = "bba" which is anagram of s.
Example 2:
Input: s = "leetcode", t = "practice" Output: 5 Explanation: Replace 'p', 'r', 'a', 'i' and 'c' from t with proper characters to make t anagram of s.
Example 3:
Input: s = "anagram", t = "mangaar" Output: 0 Explanation: "anagram" and "mangaar" are anagrams.
Constraints:
1 <= s.length <= 5 * 104s.length == t.lengths and t consist of lowercase English letters only.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To figure out the minimum steps to make two words anagrams using a brute force method, you would exhaustively explore every possible way to swap or change letters until both words contain the exact same letters in some order. Since this is the most exhaustive approach, you basically test all potential modifications to see which one gets the job done with the fewest changes.
Here's how the algorithm would work step-by-step:
def min_steps(first_string, second_string):
from collections import deque
alphabet_letters = 'abcdefghijklmnopqrstuvwxyz'
queue_for_search = deque([(first_string, 0)])
visited_states = {first_string}
# We use a breadth-first search to guarantee finding the shortest modification path first.
while queue_for_search:
current_word, current_steps = queue_for_search.popleft()
if sorted(current_word) == sorted(second_string):
return current_steps
# Generate all possible single-character modifications to explore every branch thoroughly.
for index in range(len(current_word)):
for replacement in alphabet_letters:
modified_word = current_word[:index] + replacement + current_word[index + 1:]
if modified_word not in visited_states:
visited_states.add(modified_word)
queue_for_search.append((modified_word, current_steps + 1))
return 0To find the minimum number of steps to make two words anagrams, we need to focus on what letters are missing or extra. By simply counting how many times each letter appears in both words, we can easily figure out how many changes are needed.
Here's how the algorithm would work step-by-step:
def min_steps_to_anagram(first_word, second_word):
# We count character frequencies to establish a baseline for comparison.
first_word_counts = [0] * 26
for character in first_word:
first_word_counts[ord(character) - ord('a')] += 1
second_word_counts = [0] * 26
for character in second_word:
second_word_counts[ord(character) - ord('a')] += 1
# Summing shortages identifies exact character deficits between the two words.
total_shortages = 0
for alphabet_index in range(26):
if first_word_counts[alphabet_index] > second_word_counts[alphabet_index]:
total_shortages += first_word_counts[alphabet_index] - second_word_counts[alphabet_index]
# Total shortages directly equate to the minimum steps required for anagram parity.
return total_shortages| Case | How to Handle |
|---|---|
| Strings are already exact anagrams of each other | The frequency counts will completely balance out, and the algorithm correctly returns zero steps. |
| Strings share no common characters at all | Every character in t must be replaced, so the algorithm will sum all frequencies and return the full string length. |
| Minimum length strings of size one with differing characters | The frequency array handles single-character bounds properly and returns one replacement step. |
| Inputs contain only identical characters such as s equals aaa and t equals bbb | The bucket counts will aggregate all occurrences into a single index and compute the difference efficiently. |
| Maximum constraint string lengths up to fifty thousand characters | Using a fixed-size frequency array or hash map guarantees an optimal linear time complexity of O(n). |
| Characters are restricted to lowercase English letters only | A fixed-size array of size twenty-six maps characters directly without needing complex hashing logic. |
| String t contains excess characters not present in s | The counting logic only accumulates positive discrepancies for characters that exceed s frequencies. |