Minimizing Steps to Form Anagrams from Given Strings
Last Updated :
28 Dec, 2023
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.
C++
#include <iostream>
#include <unordered_map>
class MinSteps {
public :
int minSteps(std::string s1, std::string s2) {
std::unordered_map< char , int > map;
int countofT = 0;
for ( char ch : s1) {
map[ch] = map[ch] + 1;
}
for ( char ch : s2) {
if (map.find(ch) != map.end()) {
if (map[ch] == 1) {
map.erase(ch);
} else {
map[ch] = map[ch] - 1;
}
} else {
countofT++;
}
}
int count = 0;
for ( const auto & kv : map) {
count += kv.second;
}
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;
}
|
Java
import java.io.*;
import java.util.HashMap;
class MinSteps {
int minSteps(String s1, String s2)
{
HashMap<Character, Integer> map = new HashMap<>();
int countofT = 0 ;
for ( char ch : s1.toCharArray()) {
map.put(ch, map.getOrDefault(ch, 0 ) + 1 );
}
for ( char ch : s2.toCharArray()) {
if (map.containsKey(ch)) {
if (map.get(ch) == 1 ) {
map.remove(ch);
}
else {
map.put(ch, map.get(ch) - 1 );
}
}
else {
countofT++;
}
}
int count = 0 ;
for ( int val : map.values()) {
count += val;
}
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));
}
}
|
Python3
class MinSteps:
def min_steps( self , s1, s2):
map = {}
count_of_t = 0
for ch in s1:
map [ch] = map .get(ch, 0 ) + 1
for ch in s2:
if ch in map :
if map [ch] = = 1 :
map .pop(ch)
else :
map [ch] - = 1
else :
count_of_t + = 1
count = 0
for val in map .values():
count + = val
return count + count_of_t
if __name__ = = "__main__" :
obj = MinSteps()
s1 = "geeksforgeeks"
s2 = "geeks"
print (obj.min_steps(s1, s2))
|
C#
using System;
using System.Collections.Generic;
class MinSteps
{
int GetMinSteps( string s1, string s2)
{
Dictionary< char , int > map = new Dictionary< char , int >();
int countofT = 0;
foreach ( char ch in s1)
{
if (map.ContainsKey(ch))
{
map[ch]++;
}
else
{
map[ch] = 1;
}
}
foreach ( char ch in s2)
{
if (map.ContainsKey(ch))
{
if (map[ch] == 1)
{
map.Remove(ch);
}
else
{
map[ch]--;
}
}
else
{
countofT++;
}
}
int count = 0;
foreach ( int val in map.Values)
{
count += val;
}
return count + countofT;
}
static void Main( string [] args)
{
MinSteps obj = new MinSteps();
string s1 = "geeksforgeeks" ;
string s2 = "geeks" ;
Console.WriteLine(obj.GetMinSteps(s1, s2));
}
}
|
Javascript
class GFG {
minSteps(s1, s2) {
const map = new Map();
let countofT = 0;
for (const ch of s1) {
map.set(ch, (map.get(ch) || 0) + 1);
}
for (const ch of s2) {
if (map.has(ch)) {
if (map.get(ch) === 1) {
map. delete (ch);
} else {
map.set(ch, map.get(ch) - 1);
}
} else {
countofT++;
}
}
let count = 0;
for (const val of map.values()) {
count += val;
}
return count + countofT;
}
}
const obj = new GFG();
const s1 = "geeksforgeeks" ;
const s2 = "geeks" ;
console.log(obj.minSteps(s1, s2));
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...