Find Minimum Number of Steps to Make Two Strings Anagram II Last Updated : 14 Jun, 2024 Comments Improve Suggest changes Like Article Like Report Given two strings S and T, the task is to find the minimum number of steps to make S and T anagrams of each other. In one step, we can append any character to either S or T. Example: Input: S = "gee", T = "eks" Output: 4Explanation: Append 'k' and 's' to string S and append 'g' and 'e' to string T to make S and T anagram of each other. Input: S = "geeks", T = "egesk"Output: 0Explanation: "geeks" and "egesk" are anagrams. Approach: To solve the problem, follow the below idea: To solve this problem, the idea is to count the frequency of each character in both strings S and T. Then, calculate the difference in frequencies for each character. The sum of positive differences will give the minimum number of steps needed to make T an anagram of S. Step-by-step algorithm: Count the frequency of each character in both strings S and T.For each character, calculate the difference in frequency between S and T.Sum the positive differences to get the minimum number of steps required.Below is the implementation of the algorithm: C++ #include <bits/stdc++.h> using namespace std; // Function to find minimum steps to make S and T anagram of // each other int minSteps(string S, string T) { // Frequency array to store the difference between // frequency of characters int freq[26] = {}; // For every character in S, add 1 to the frequency // array for (char ch : S) { freq[ch - 'a'] += 1; } // For every character in S, subtract 1 to the frequency // array for (char ch : T) { freq[ch - 'a'] -= 1; } // Sum of absolute difference is the required number of // steps int ans = 0; for (int i = 0; i < 26; i++) { ans += abs(freq[i]); } return ans; } int main() { // Sample Input string S = "gee"; string T = "eks"; cout << minSteps(S, T) << endl; return 0; } Java import java.util.*; public class MinStepsAnagram { // Function to find minimum steps to make S and T // anagram of each other public static int minSteps(String S, String T) { // Frequency array to store the difference between // frequency of characters int[] freq = new int[26]; // For every character in S, add 1 to the frequency // array for (char ch : S.toCharArray()) { freq[ch - 'a'] += 1; } // For every character in T, subtract 1 from the // frequency array for (char ch : T.toCharArray()) { freq[ch - 'a'] -= 1; } // Sum of absolute differences is the required // number of steps int ans = 0; for (int i = 0; i < 26; i++) { ans += Math.abs(freq[i]); } return ans; } public static void main(String[] args) { // Sample Input String S = "gee"; String T = "eks"; System.out.println(minSteps(S, T)); } } // This code is contributed by Shivam Gupta Output4 Time Complexity: O(n), where n is the length of the strings S and T. Auxiliary Space: O(1), since the size of the frequency arrays is constant (26 for each alphabet letter). Comment More infoAdvertise with us Next Article Find Minimum Number of Steps to Make Two Strings Anagram II C codernitnucy Follow Improve Article Tags : DSA Arrays Bloomberg Interview-Questions Practice Tags : BloombergArrays Similar Reads Find the minimum number of preprocess moves required to make two strings equal Given two strings A and B of equal lengths consisting of lower case English letters. The task is to count the minimum number of pre-processing moves on string A required to make it equal to string B after applying below operations: Choose any index i (0 ? i < n) and swap characters ai and bi.Choo 9 min read Minimum number of given operations required to make two strings equal Given two strings A and B, both strings contain characters a and b and are of equal lengths. There is one _ (empty space) in both the strings. The task is to convert first string into second string by doing the minimum number of the following operations: If _ is at position i then _ can be swapped w 15 min read Minimizing Steps to Form Anagrams from Given Strings Given two strings s1 and s2. You have the flexibility to add any letter to either the string s1 or s2 in just one action. Find out the least number of steps needed to transform two given words, s1, and s2, into anagrams of each other. The length of both strings can be different and it contains only 7 min read Remove minimum characters so that two strings become anagram Given two strings in lowercase, the task is to make them anagrams. The only allowed operation is to remove a character from any string. Find the minimum number of characters to be deleted to make both the strings anagram. Two strings are called anagrams of each other if one of them can be converted 12 min read Using Counter() in Python to find minimum character removal to make two strings anagram Given two strings in lowercase, the task is to make them Anagram. The only allowed operation is to remove a character from any string. Find minimum number of characters to be deleted to make both the strings anagram? If two strings contains same data set in any order then strings are called Anagrams 3 min read Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character | Set 2 Given two equal-size strings s[] and t[] of size N. In one step, choose any character of t[] and replace it with another character. Return the minimum number of steps to make t[] an anagram of s[]. Note: An Anagram of a string is a string that contains the same characters with a different (or the sa 6 min read Minimum number of adjacent swaps to convert a string into its given anagram Given two strings s1 and s2, the task is to find the minimum number of steps required to convert s1 into s2. The only operation allowed is to swap adjacent elements in the first string. Every swap is counted as a single step.Examples: Input: s1 = "abcd", s2 = "cdab" Output: 4 Swap 2nd and 3rd elemen 8 min read Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character in JavaScript In this article, we are going to learn how we will find the minimum number of manipulations required to make the two strings str1 and str2 an anagram without deleting any character from both strings. Note that two strings are called an anagram of each other only if one string is a permutation of the 5 min read Transform One String to Another using Minimum Number of Given Operation Given two strings A and B, the task is to convert A to B if possible. The only operation allowed is to put any character from A and insert it at front. Find if it's possible to convert the string. If yes, then output minimum no. of operations required for transformation.Examples: Input: A = "ABD", B 15+ min read Like