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 lowercase English letters.
Note: The anagram strings use the same letters, but the sequence of characters can be in different order.
Examples:
Input: s1 = “geeks”, s2 = “skege”
Output : 0
Explanation : Both strings s1 and s2 contains same characters.Input: s1 = “geeksforgeeks”, s2 = “geeks”
Output : 8
Explanation : In this case, we need to append “forgeeks” to s1 in 8 steps. Now “geeksforgeeks” and “geeks” are anagram of each other.
Find the least number of steps required to transform two given words, s, and t, into anagrams of each other using the map.
The idea is to consider all the characters that are not common to both string. We can use map and a variable to achieve this.
Follow the steps given below to implement the approach:
- Initialize the map to maintain the character frequencies in “s1” string.
- Iterate through the string “s1” and update the respective count of characters in the map.
- Take a variable countofT which, is used to keep track of characters in “s2” strhasthat have no corresponding counterparts in “s1”.
- Then iterate through the “s2” string. For character found in the map, reduce their count and if the count become 0 then simply remove that character from the map else reduce the count of that character by 1.
- Also while iterating through the “s2” string, take care of the character which is not present in map. countofT is incremented to account for the extra character present in the “s2” string.
- After processing both strings, there may be some remaining character in the map, which represents those characters in the characters “s1” string that lacks corresponding characterin “s2” string and countofT which represents the extra character in the “s2” which is not there in “s1”.
- Now combine the count of the remaining character in the map and countofT which represents the extra character in “s2” which is not there in “s1”. This combined count tells the least number of steps required to transform two given words, “s1” and “s2”, into anagrams of each other.
- Return the combined count.
Below is the implementation of the above approach.
#include <iostream> #include <unordered_map> class MinSteps {
public :
int minSteps(std::string s1, std::string s2) {
// Initialize map
std::unordered_map< char , int > map;
int countofT = 0; // Initialize countofT
// Traverse through string s1 and put frequencies of
// each character in the map
for ( char ch : s1) {
map[ch] = map[ch] + 1;
}
// Traverse through string s2
for ( char ch : s2) {
if (map.find(ch) != map.end()) {
// If the map contains the current character of s2
// and its frequency is 1, then remove the character
if (map[ch] == 1) {
map.erase(ch);
} else {
// Reduce the frequency of the character by 1
map[ch] = map[ch] - 1;
}
} else {
// Extra character in s2 which is not there in s1
countofT++;
}
}
int count = 0;
// Iterate through the values of the map and count the
// frequencies
for ( const auto & kv : map) {
count += kv.second;
}
// Combine the count and return
return count + countofT;
}
}; int main() {
MinSteps obj;
std::string s1 = "geeksforgeeks" ;
std::string s2 = "geeks" ;
std::cout << obj.minSteps(s1, s2) << std::endl;
return 0;
} // This code is contributed by rambabuguphka |
/*package whatever //do not write package name here */ import java.io.*;
import java.util.HashMap;
class MinSteps {
int minSteps(String s1, String s2)
{
// initialize map
HashMap<Character, Integer> map = new HashMap<>();
int countofT = 0 ; // initialize countofT
// traverse through string s and put frequnecies of
// each character in map
for ( char ch : s1.toCharArray()) {
map.put(ch, map.getOrDefault(ch, 0 ) + 1 );
}
// traverse through string t
for ( char ch : s2.toCharArray()) {
if (map.containsKey(ch)) {
// if map contains current character of t
// and its frequency is 1 the remove the
// character
if (map.get(ch) == 1 ) {
map.remove(ch);
}
else {
// reduce the frequency of charcater by
// 1
map.put(ch, map.get(ch) - 1 );
}
}
else { // extra character in s2 which is not
// there in s1
countofT++;
}
}
int count = 0 ;
// iterate through the values of map and count the
// frquencies
for ( int val : map.values()) {
count += val;
}
// combine the count and return
return count + countofT;
}
public static void main(String[] args)
{
MinSteps obj = new MinSteps();
String s1 = "geeksforgeeks";
String s2 = "geeks";
System.out.println(obj.minSteps(s1, s2));
}
} |
class MinSteps:
def min_steps( self , s1, s2):
# initialize map
map = {}
count_of_t = 0 # initialize count_of_t
# traverse through string s and put frequencies of
# each character in map
for ch in s1:
map [ch] = map .get(ch, 0 ) + 1
# traverse through string t
for ch in s2:
if ch in map :
# if map contains current character of t
# and its frequency is 1, then remove the
# character
if map [ch] = = 1 :
map .pop(ch)
else :
# reduce the frequency of the character by
# 1
map [ch] - = 1
else :
# extra character in s2 which is not
# there in s1
count_of_t + = 1
count = 0
# iterate through the values of map and count the
# frequencies
for val in map .values():
count + = val
# combine the count and return
return count + count_of_t
if __name__ = = "__main__" :
obj = MinSteps()
s1 = "geeksforgeeks"
s2 = "geeks"
print (obj.min_steps(s1, s2))
|
// C# Implementation using System;
using System.Collections.Generic;
class MinSteps
{ int GetMinSteps( string s1, string s2)
{
// initialize dictionary
Dictionary< char , int > map = new Dictionary< char , int >();
int countofT = 0; // initialize countofT
// traverse through string s1 and put frequencies of
// each character in map
foreach ( char ch in s1)
{
if (map.ContainsKey(ch))
{
map[ch]++;
}
else
{
map[ch] = 1;
}
}
// traverse through string s2
foreach ( char ch in s2)
{
if (map.ContainsKey(ch))
{
// if map contains current character of s2
// and its frequency is 1 then remove the
// character
if (map[ch] == 1)
{
map.Remove(ch);
}
else
{
// reduce the frequency of character by 1
map[ch]--;
}
}
else
{
// extra character in s2 which is not
// there in s1
countofT++;
}
}
int count = 0;
// iterate through the values of map and count the
// frequencies
foreach ( int val in map.Values)
{
count += val;
}
// combine the count and return
return count + countofT;
}
static void Main( string [] args)
{
MinSteps obj = new MinSteps();
string s1 = "geeksforgeeks" ;
string s2 = "geeks" ;
Console.WriteLine(obj.GetMinSteps(s1, s2));
}
} // This code is contributed by Sakshi |
class GFG { minSteps(s1, s2) {
// Initialize map
const map = new Map();
let countofT = 0;
// Traverse through string s1 and put frequencies of the each character in map
for (const ch of s1) {
map.set(ch, (map.get(ch) || 0) + 1);
}
// Traverse through string s2
for (const ch of s2) {
if (map.has(ch)) {
if (map.get(ch) === 1) {
map. delete (ch);
} else {
// Reduce the frequency of character by 1
map.set(ch, map.get(ch) - 1);
}
} else {
// Extra character in s2 which is not there in s1
countofT++;
}
}
let count = 0;
// Iterate through the values of the map and count the frequencies
for (const val of map.values()) {
count += val;
}
// Combine the count and return
return count + countofT;
}
} // Driver Code const obj = new GFG();
const s1 = "geeksforgeeks" ;
const s2 = "geeks" ;
console.log(obj.minSteps(s1, s2)); |
8
Time Complexity: O(N)
Auxiliary Space: O(N)