# Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character | Set 2

• Difficulty Level : Easy
• Last Updated : 28 Dec, 2022

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 same) ordering.

Examples:

Input: s = “baa”, t = “aba”
Output: 0
Explanation: Both String contains identical characters

Input: s = “ddcf”, t = “cedk”
Output: 2
Explanation: Here, we need to change two characters in either of the strings to make them identical. We can change ‘d’ and ‘f’ in s1 or ‘e’ and ‘k’ in s2.

Hashing Approach: The hashing approach has been discussed in Set 1 of this article. In this article, we will be looking at how to solve this problem using the map.

Approach: The idea is to use Hashing to store the frequencies of every character of the string s[] and then while traversing string t[], check if that character is present in the map or not. If yes, then decrease its value by 1. Else, increase the count by 1. Follow the steps below to solve the problem:

Below is the implementation of the above approach.

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to count the minimum changes``// to make the 2 strings anagram``int` `minSteps(string s, string t)``{``    ``unordered_map<``char``, ``int``> a;` `    ``// For counting the steps to be changed``    ``int` `count = 0;``    ``for` `(``auto` `i : s) {` `        ``// Increasing the count if the no.``        ``// is present``        ``a[i]++;``    ``}``    ``for` `(``auto` `j : t) {` `        ``// Checking if the element of s is``        ``// present in t or not``        ``if` `(a[j] > 0) {` `            ``// If present then decrease the``            ``// no. in map by 1``            ``a[j]--;``        ``}``        ``else` `{` `            ``// If not present``            ``// increase count by 1``            ``count++;``        ``}``    ``}``    ``cout << count;` `    ``// Return count``    ``return` `count;``}` `// Driver Code``int` `main()``{``    ``string s = ``"ddcf"``, t = ``"cedk"``;` `    ``minSteps(s, t);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG {` `  ``// Function to count the minimum changes``  ``// to make the 2 Strings anagram``  ``static` `int` `minSteps(String s, String t) {``    ``HashMap a =``      ``new` `HashMap();` `    ``// For counting the steps to be changed``    ``int` `count = ``0``;``    ``for` `(``char` `i : s.toCharArray()) {` `      ``// Increasing the count if the no.``      ``// is present``      ``if` `(a.containsKey(i)) {``        ``a.put(i, a.get(i) + ``1``);``      ``} ``else` `{``        ``a.put(i, ``1``);``      ``}` `    ``}``    ``for` `(``char` `j : t.toCharArray()) {` `      ``// Checking if the element of s is``      ``// present in t or not``      ``if` `(a.containsKey(j)) {` `        ``// If present then decrease the``        ``// no. in map by 1``        ``a.put(j, a.get(j) - ``1``);``      ``} ``else` `{` `        ``// If not present``        ``// increase count by 1``        ``count++;``      ``}``    ``}``    ``System.out.print(count);` `    ``// Return count``    ``return` `count;``  ``}` `  ``// Driver Code``  ``public` `static` `void` `main(String[] args) {``    ``String s = ``"ddcf"``, t = ``"cedk"``;` `    ``minSteps(s, t);``  ``}``}` `// This code is contributed by shikhasingrajput`

## Python3

 `# python program for the above approach` `# Function to count the minimum changes``# to make the 2 strings anagram``def` `minSteps(s, t):` `    ``a ``=` `{}` `    ``# For counting the steps to be changed``    ``count ``=` `0` `    ``for` `i ``in` `s:` `        ``# Increasing the count if the no.``        ``# is present``        ``if` `i ``in` `a:``            ``a[i] ``+``=` `1``        ``else``:``            ``a[i] ``=` `1` `    ``for` `j ``in` `t:` `                ``# Checking if the element of s is``                ``# present in t or not``        ``if` `j ``in` `a:` `                        ``# If present then decrease the``                        ``# no. in map by 1``            ``a[j] ``-``=` `1` `        ``else``:` `            ``# If not present``            ``# increase count by 1``            ``count ``+``=` `1` `    ``print``(count)` `    ``# Return count``    ``return` `count` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``s ``=` `"ddcf"``    ``t ``=` `"cedk"` `    ``minSteps(s, t)` `    ``# This code is contributed by rakeshsahni`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `public` `class` `GFG {` `  ``// Function to count the minimum changes``  ``// to make the 2 Strings anagram``  ``static` `int` `minSteps(String s, String t) {``    ``Dictionary<``char``, ``int``> a =``      ``new` `Dictionary<``char``, ``int``>();` `    ``// For counting the steps to be changed``    ``int` `count = 0;``    ``foreach` `(``char` `i ``in` `s.ToCharArray()) {` `      ``// Increasing the count if the no.``      ``// is present``      ``if` `(a.ContainsKey(i)) {``        ``a[i] = a[i] + 1;``      ``} ``else` `{``        ``a.Add(i, 1);``      ``}` `    ``}``    ``foreach` `(``char` `j ``in` `t.ToCharArray()) {` `      ``// Checking if the element of s is``      ``// present in t or not``      ``if` `(a.ContainsKey(j)) {` `        ``// If present then decrease the``        ``// no. in map by 1``        ``a[j] = a[j] - 1;``      ``} ``else` `{` `        ``// If not present``        ``// increase count by 1``        ``count++;``      ``}``    ``}``    ``Console.Write(count);` `    ``// Return count``    ``return` `count;``  ``}` `  ``// Driver Code``  ``public` `static` `void` `Main(String[] args) {``    ``String s = ``"ddcf"``, t = ``"cedk"``;` `    ``minSteps(s, t);``  ``}``}` `// This code is contributed by shikhasingrajput`

## Javascript

 ``

Output

`2`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up