1347. Minimum Number of Steps to Make Two Strings Anagram LeetCode Solution
In this guide, you will get 1347. Minimum Number of Steps to Make Two Strings Anagram LeetCode Solution with the best time and space complexity. The solution to Minimum Number of Steps to Make Two Strings Anagram problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Number of Steps to Make Two Strings Anagram solution in C++
- Minimum Number of Steps to Make Two Strings Anagram solution in Java
- Minimum Number of Steps to Make Two Strings Anagram solution in Python
- Additional Resources

Problem Statement of Minimum Number of Steps to Make Two Strings Anagram
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 * 104
s.length == t.length
s and t consist of lowercase English letters only.
Complexity Analysis
- Time Complexity: O(|\texttt{s}| + |\texttt{t}|)
- Space Complexity: O(26) = O(1)
1347. Minimum Number of Steps to Make Two Strings Anagram LeetCode Solution in C++
class Solution {
public:
int minSteps(string s, string t) {
vector<int> count(26);
for (const char c : s)
++count[c - 'a'];
for (const char c : t)
--count[c - 'a'];
return accumulate(count.begin(), count.end(), 0, [](int subtotal, int c) {
return subtotal + abs(c);
}) / 2;
}
};
/* code provided by PROGIEZ */
1347. Minimum Number of Steps to Make Two Strings Anagram LeetCode Solution in Java
class Solution {
public int minSteps(String s, String t) {
int[] count = new int[26];
for (final char c : s.toCharArray())
++count[c - 'a'];
for (final char c : t.toCharArray())
--count[c - 'a'];
return Arrays.stream(count).map(Math::abs).sum() / 2;
}
}
// code provided by PROGIEZ
1347. Minimum Number of Steps to Make Two Strings Anagram LeetCode Solution in Python
class Solution:
def minSteps(self, s: str, t: str) -> int:
count = collections.Counter(s)
count.subtract(collections.Counter(t))
return sum(abs(value) for value in count.values()) // 2
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.