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:
- Initialize the variable count as 0 to store the answer.
- Initialize an unordered_map<char, int> a[] to store the frequencies.
- Iterate over the range [0, N) using the variable i and perform the following steps:
- Traverse over the string using the variable j and perform the following tasks:
- If a[j] is greater than 0 then decrease the value of a[j] by 1.
- Else, increase the value of count by 1.
- After performing the above steps, print the value of count as the answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int minSteps(string s, string t)
{
unordered_map< char , int > a;
int count = 0;
for ( auto i : s) {
a[i]++;
}
for ( auto j : t) {
if (a[j] > 0) {
a[j]--;
}
else {
count++;
}
}
cout << count;
return count;
}
int main()
{
string s = "ddcf" , t = "cedk" ;
minSteps(s, t);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int minSteps(String s, String t) {
HashMap<Character, Integer> a =
new HashMap<Character, Integer>();
int count = 0 ;
for ( char i : s.toCharArray()) {
if (a.containsKey(i)) {
a.put(i, a.get(i) + 1 );
} else {
a.put(i, 1 );
}
}
for ( char j : t.toCharArray()) {
if (a.containsKey(j)) {
a.put(j, a.get(j) - 1 );
} else {
count++;
}
}
System.out.print(count);
return count;
}
public static void main(String[] args) {
String s = "ddcf" , t = "cedk" ;
minSteps(s, t);
}
}
|
Python3
def minSteps(s, t):
a = {}
count = 0
for i in s:
if i in a:
a[i] + = 1
else :
a[i] = 1
for j in t:
if j in a:
a[j] - = 1
else :
count + = 1
print (count)
return count
if __name__ = = "__main__" :
s = "ddcf"
t = "cedk"
minSteps(s, t)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static int minSteps(String s, String t) {
Dictionary< char , int > a =
new Dictionary< char , int >();
int count = 0;
foreach ( char i in s.ToCharArray()) {
if (a.ContainsKey(i)) {
a[i] = a[i] + 1;
} else {
a.Add(i, 1);
}
}
foreach ( char j in t.ToCharArray()) {
if (a.ContainsKey(j)) {
a[j] = a[j] - 1;
} else {
count++;
}
}
Console.Write(count);
return count;
}
public static void Main(String[] args) {
String s = "ddcf" , t = "cedk" ;
minSteps(s, t);
}
}
|
Javascript
<script>
function minSteps(s, t)
{
let a = new Map();
let count = 0;
for (let i of s) {
if (a.has(i)) {
a.set(i, 1)
}
else {
a.set(i, a.get(i) + 1)
}
}
for (let j of t) {
if (a.has(j)) {
a.set(j, a.get(j) - 1);
}
else {
count++;
}
}
document.write(count);
return count;
}
let s = "ddcf" , t = "cedk" ;
minSteps(s, t);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)