Skip to main content

Minimum number of deletion required to make two strings anagram

Here given code implementation process.

// Include header file
#include <iostream>
#include <string>
#include <unordered_map>

using namespace std;
/*
  C++ program for
  Minimum number of deletion required to make two strings anagram
*/
class Anagrams
{
    public: void countFrequency(string str, unordered_map < char, int > &record)
    {
        int n = str.length();
        // Calculate frequency of given string
        for (int i = 0; i < n; ++i)
        {
            if (record.find(str[i]) != record.end())
            {
                record[str[i]] = record[str[i]] + 1;
            }
            else
            {
                record[str[i]] = 1;
            }
        }
    }
    void makeAnagram(string str1, string str2)
    {
        int operation = 0;
        unordered_map < char, int > record1;
        unordered_map < char, int > record2;
        // Count frequency of given string
        this->countFrequency(str1, record1);
        this->countFrequency(str2, record2);
        int k1 = 0;
        int k2 = 0;
        // Compare same frequency in both string
        for (auto &info: record1)
        {
            if (record2.find(info.first) != record2.end())
            {
                k1 = info.second;
                k2 = record2[info.first];
                if (k1 > k2)
                {
                    operation += k1 - k2;
                }
                else if (k2 > k1)
                {
                    operation += k2 - k1;
                }
                // Reset frequency value
                record1[info.first] = 0;
                record2[info.first] = 0;
            }
        }
        // Add number of extra characters in str1
        for (auto &info: record1)
        {
            operation += info.second;
        }
        // Add number of extra characters in str2
        for (auto &info: record2)
        {
            operation += info.second;
        }
        cout << " Given (" << str1 << "),(" << str2 << ")" << endl;
        // Display number of remove operation to make anagram
        cout << " Result : " << operation << endl;
    }
};
int main()
{
    Anagrams *task = new Anagrams();
    /*
        Case A
        ------
        k m e t e o r o i l , r e m f i l l o t e
        ↓             ↓             ↓     ↓
        ①            ②             ③    ④
        ------------------------------------------------
        Remove 4 characters to make both string is anagram
    */
    task->makeAnagram("kmeteoroil", "remfillote");
    /*
        Case B
        ------
        C  S I T W I  N  , W  S  O  I  C  N  L  P
               ↓   ↓             ↓           ↓  ↓
               ①  ②             ③          ④  ⑤
        ----------------------------------------------
        Remove 5 characters to make both string is anagram
    */
    task->makeAnagram("CSITWIN", "WSOICNLP");
    /*
        Case C
        ------
        Both string is anagram
    */
    task->makeAnagram("123", "321");
    return 0;
}

Output

 Given (kmeteoroil),(remfillote)
 Result : 4
 Given (CSITWIN),(WSOICNLP)
 Result : 5
 Given (123),(321)
 Result : 0
import java.util.HashMap;
/*
  Java program for
  Minimum number of deletion required to make two strings anagram
*/
public class Anagrams
{
	public void countFrequency(String str, HashMap < Character, Integer > record)
	{
		int n = str.length();
		// Calculate frequency of given string
		for (int i = 0; i < n; ++i)
		{
			if (record.containsKey(str.charAt(i)))
			{
				record.put(str.charAt(i), record.get(str.charAt(i)) + 1);
			}
			else
			{
				record.put(str.charAt(i), 1);
			}
		}
	}
	public void makeAnagram(String str1, String str2)
	{
		int operation = 0;
		HashMap < Character, Integer > record1 = new HashMap < Character, Integer > ();
		HashMap < Character, Integer > record2 = new HashMap < Character, Integer > ();
		// Count frequency of given string
		countFrequency(str1, record1);
		countFrequency(str2, record2);
		int k1 = 0;
		int k2 = 0;
		// Compare same frequency in both string
		for (char info: record1.keySet())
		{
			if (record2.containsKey(info))
			{
				k1 = record1.get(info);
				k2 = record2.get(info);
				if (k1 > k2)
				{
					operation += k1 - k2;
				}
				else if (k2 > k1)
				{
					operation += k2 - k1;
				}
				// Reset frequency value
				record1.put(info, 0);
				record2.put(info, 0);
			}
		}
		// Add number of extra characters in str1
		for (char info: record1.keySet())
		{
			operation += record1.get(info);
		}
		// Add number of extra characters in str2
		for (char info: record2.keySet())
		{
			operation += record2.get(info);
		}
		System.out.println(" Given (" + str1 + "),(" + str2 + ")");
		// Display number of remove operation to make anagram
		System.out.println(" Result : " + operation);
	}
	public static void main(String[] args)
	{
		Anagrams task = new Anagrams();
		/*
		    Case A
		    ------
		    k m e t e o r o i l , r e m f i l l o t e
		    ↓             ↓             ↓     ↓
		    ①            ②             ③    ④
		    ------------------------------------------------
		    Remove 4 characters to make both string is anagram
		*/
		task.makeAnagram("kmeteoroil", "remfillote");
		/*
		    Case B
		    ------
		    C  S I T W I  N  , W  S  O  I  C  N  L  P
		           ↓   ↓             ↓           ↓  ↓
		           ①  ②             ③          ④  ⑤
		    ----------------------------------------------
		    Remove 5 characters to make both string is anagram
		*/
		task.makeAnagram("CSITWIN", "WSOICNLP");
		/*
		    Case C
		    ------
		    Both string is anagram
		*/
		task.makeAnagram("123", "321");
	}
}

Output

 Given (kmeteoroil),(remfillote)
 Result : 4
 Given (CSITWIN),(WSOICNLP)
 Result : 5
 Given (123),(321)
 Result : 0
// Include namespace system
using System;
using System.Collections.Generic;
/*
  Csharp program for
  Minimum number of deletion required to make two strings anagram
*/
public class Anagrams
{
	public void countFrequency(String str, Dictionary < char, int > record)
	{
		int n = str.Length;
		// Calculate frequency of given string
		for (int i = 0; i < n; ++i)
		{
			if (record.ContainsKey(str[i]))
			{
				record[str[i]] = record[str[i]] + 1;
			}
			else
			{
				record.Add(str[i], 1);
			}
		}
	}
	public void makeAnagram(String str1, String str2)
	{
		int operation = 0;
		Dictionary < char, int > record1 = 
          new Dictionary < char, int > ();
		Dictionary < char, int > record2 = 
          new Dictionary < char, int > ();
		// Count frequency of given string
		this.countFrequency(str1, record1);
		this.countFrequency(str2, record2);
		int k1 = 0;
		int k2 = 0;
      	List<char> keys = new List<char>(record1.Keys);
		// Compare same frequency in both string
		foreach(char key in keys)
		{
			if (record2.ContainsKey(key))
			{
				k1 = record1[key];
				k2 = record2[key];
				if (k1 > k2)
				{
					operation += k1 - k2;
				}
				else if (k2 > k1)
				{
					operation += k2 - k1;
				}
				// Reset frequency value
				record1[key] = 0;
				record2[key] = 0;
			}
		}
		// Add number of extra characters in str1
		foreach(KeyValuePair < char, int > info in record1)
		{
			operation += info.Value;
		}
		// Add number of extra characters in str2
		foreach(KeyValuePair < char, int > info in record2)
		{
			operation += info.Value;
		}
		Console.WriteLine(" Given (" + str1 + "),(" + str2 + ")");
		// Display number of remove operation to make anagram
		Console.WriteLine(" Result : " + operation);
	}
	public static void Main(String[] args)
	{
		Anagrams task = new Anagrams();
		/*
		    Case A
		    ------
		    k m e t e o r o i l , r e m f i l l o t e
		    ↓             ↓             ↓     ↓
		    ①            ②             ③    ④
		    ------------------------------------------------
		    Remove 4 characters to make both string is anagram
		*/
		task.makeAnagram("kmeteoroil", "remfillote");
		/*
		    Case B
		    ------
		    C  S I T W I  N  , W  S  O  I  C  N  L  P
		           ↓   ↓             ↓           ↓  ↓
		           ①  ②             ③          ④  ⑤
		    ----------------------------------------------
		    Remove 5 characters to make both string is anagram
		*/
		task.makeAnagram("CSITWIN", "WSOICNLP");
		/*
		    Case C
		    ------
		    Both string is anagram
		*/
		task.makeAnagram("123", "321");
	}
}

Output

 Given (kmeteoroil),(remfillote)
 Result : 4
 Given (CSITWIN),(WSOICNLP)
 Result : 5
 Given (123),(321)
 Result : 0
package main
import "fmt"
/*
  Go program for
  Minimum number of deletion required to make two strings anagram
*/

func countFrequency(str string, record map[byte]int) {
	var n int = len(str)
	// Calculate frequency of given string
	for i := 0 ; i < n ; i++ {
		if _, found := record[str[i]] ; found {
			record[str[i]] = record[str[i]] + 1
		} else {
			record[str[i]] = 1
		}
	}
}
func makeAnagram(str1, str2 string) {
	var operation int = 0
	var record1 = make(map[byte] int)
	var record2 = make(map[byte] int)
	// Count frequency of given string
	countFrequency(str1, record1)
	countFrequency(str2, record2)
	var k1 int = 0
	var k2 int = 0
	// Compare same frequency in both string
	for k, v := range record1 {
		if _, found := record2[k] ; found {
			k1 = v
			k2 = record2[k]
			if k1 > k2 {
				operation += k1 - k2
			} else if k2 > k1 {
				operation += k2 - k1
			}
			// Reset frequency value
			record1[k] = 0
			record2[k] = 0
		}
	}
	// Add number of extra characters in str1
	for _, v := range record1 {
		operation += v
	}
	// Add number of extra characters in str2
	for _, v := range record2 {
		operation += v
	}
	fmt.Println(" Given (", str1, "),(", str2, ")")
	// Display number of remove operation to make anagram
	fmt.Println(" Result : ", operation)
}
func main() {

	/*
	    Case A
	    ------
	    k m e t e o r o i l , r e m f i l l o t e
	    ↓             ↓             ↓     ↓
	    ①            ②             ③    ④
	    ------------------------------------------------
	    Remove 4 characters to make both string is anagram
	*/
	makeAnagram("kmeteoroil", "remfillote")
	/*
	    Case B
	    ------
	    C  S I T W I  N  , W  S  O  I  C  N  L  P
	           ↓   ↓             ↓           ↓  ↓
	           ①  ②             ③          ④  ⑤
	    ----------------------------------------------
	    Remove 5 characters to make both string is anagram
	*/
	makeAnagram("CSITWIN", "WSOICNLP")
	/*
	    Case C
	    ------
	    Both string is anagram
	*/
	makeAnagram("123", "321")
}

Output

 Given (kmeteoroil),(remfillote)
 Result : 4
 Given (CSITWIN),(WSOICNLP)
 Result : 5
 Given (123),(321)
 Result : 0
<?php
/*
  Php program for
  Minimum number of deletion required to make two strings anagram
*/
class Anagrams
{
	public	function countFrequency($str, &$record)
	{
		$n = strlen($str);
		// Calculate frequency of given string
		for ($i = 0; $i < $n; ++$i)
		{
			if (array_key_exists($str[$i], $record))
			{
				$record[$str[$i]] = $record[$str[$i]] + 1;
			}
			else
			{
				$record[$str[$i]] = 1;
			}
		}
	}
	public	function makeAnagram($str1, $str2)
	{
		$operation = 0;
		$record1 = array();
		$record2 = array();
		// Count frequency of given string
		$this->countFrequency($str1, $record1);
		$this->countFrequency($str2, $record2);
		$k1 = 0;
		$k2 = 0;
		// Compare same frequency in both string
		foreach($record1 as $key => $value)
		{
			if (array_key_exists($key, $record2))
			{
				$k1 = $record1[$key];
				$k2 = $record2[$key];
				if ($k1 > $k2)
				{
					$operation += $k1 - $k2;
				}
				else if ($k2 > $k1)
				{
					$operation += $k2 - $k1;
				}
				// Reset frequency value
				$record1[$key] = 0;
				$record2[$key] = 0;
			}
		}
		// Add number of extra characters in str1
		foreach($record1 as $key => $value)
		{
			$operation += $value;
		}
		// Add number of extra characters in str2
		foreach($record2 as $key => $value)
		{
			$operation += $value;
		}
		echo(" Given (".$str1."),(".$str2.")\n");
		// Display number of remove operation to make anagram
		echo(" Result : ".$operation."\n");
	}
}

function main()
{
	$task = new Anagrams();
	/*
	    Case A
	    ------
	    k m e t e o r o i l , r e m f i l l o t e
	    ↓             ↓             ↓     ↓
	    ①            ②             ③    ④
	    ------------------------------------------------
	    Remove 4 characters to make both string is anagram
	*/
	$task->makeAnagram("kmeteoroil", "remfillote");
	/*
	    Case B
	    ------
	    C  S I T W I  N  , W  S  O  I  C  N  L  P
	           ↓   ↓             ↓           ↓  ↓
	           ①  ②             ③          ④  ⑤
	    ----------------------------------------------
	    Remove 5 characters to make both string is anagram
	*/
	$task->makeAnagram("CSITWIN", "WSOICNLP");
	/*
	    Case C
	    ------
	    Both string is anagram
	*/
	$task->makeAnagram("123", "321");
}
main();

Output

 Given (kmeteoroil),(remfillote)
 Result : 4
 Given (CSITWIN),(WSOICNLP)
 Result : 5
 Given (123),(321)
 Result : 0
/*
  Node JS program for
  Minimum number of deletion required to make two strings anagram
*/
class Anagrams
{
	countFrequency(str, record)
	{
		var n = str.length;
		// Calculate frequency of given string
		for (var i = 0; i < n; ++i)
		{
			if (record.has(str.charAt(i)))
			{
				record.set(str.charAt(i), record.get(str.charAt(i)) + 1);
			}
			else
			{
				record.set(str.charAt(i), 1);
			}
		}
	}
	makeAnagram(str1, str2)
	{
		var operation = 0;
		var record1 = new Map();
		var record2 = new Map();
		// Count frequency of given string
		this.countFrequency(str1, record1);
		this.countFrequency(str2, record2);
		var k1 = 0;
		var k2 = 0;
		// Compare same frequency in both string
		for (let [key, value] of record1)
		{
			if (record2.has(key))
			{
				k1 = value;
				k2 = record2.get(key);
				if (k1 > k2)
				{
					operation += k1 - k2;
				}
				else if (k2 > k1)
				{
					operation += k2 - k1;
				}
				// Reset frequency value
				record1.set(key, 0);
				record2.set(key, 0);
			}
		}
		// Add number of extra characters in str1
		for (let [key, value] of record1)
		{
			operation += value;
		}
		// Add number of extra characters in str2
		for (let [key, value] of record2)
		{
			operation += value;
		}
		console.log(" Given (" + str1 + "),(" + str2 + ")");
		// Display number of remove operation to make anagram
		console.log(" Result : " + operation);
	}
}

function main()
{
	var task = new Anagrams();
	/*
	    Case A
	    ------
	    k m e t e o r o i l , r e m f i l l o t e
	    ↓             ↓             ↓     ↓
	    ①            ②             ③    ④
	    ------------------------------------------------
	    Remove 4 characters to make both string is anagram
	*/
	task.makeAnagram("kmeteoroil", "remfillote");
	/*
	    Case B
	    ------
	    C  S I T W I  N  , W  S  O  I  C  N  L  P
	           ↓   ↓             ↓           ↓  ↓
	           ①  ②             ③          ④  ⑤
	    ----------------------------------------------
	    Remove 5 characters to make both string is anagram
	*/
	task.makeAnagram("CSITWIN", "WSOICNLP");
	/*
	    Case C
	    ------
	    Both string is anagram
	*/
	task.makeAnagram("123", "321");
}
main();

Output

 Given (kmeteoroil),(remfillote)
 Result : 4
 Given (CSITWIN),(WSOICNLP)
 Result : 5
 Given (123),(321)
 Result : 0
#  Python 3 program for
#  Minimum number of deletion required to make two strings anagram
class Anagrams :
	def countFrequency(self, str, record) :
		n = len(str)
		i = 0
		#  Calculate frequency of given string
		while (i < n) :
			if ((str[i] in record.keys())) :
				record[str[i]] = record.get(str[i]) + 1
			else :
				record[str[i]] = 1
			
			i += 1
		
	
	def makeAnagram(self, str1, str2) :
		operation = 0
		record1 = dict()
		record2 = dict()
		#  Count frequency of given string
		self.countFrequency(str1, record1)
		self.countFrequency(str2, record2)
		k1 = 0
		k2 = 0
		for key, value in record1.items() :
			if ((key in record2.keys())) :
				k1 = value
				k2 = record2.get(key)
				if (k1 > k2) :
					operation += k1 - k2
				elif (k2 > k1) :
					operation += k2 - k1
				
				#  Reset frequency value
				record1[key] = 0
				record2[key] = 0
			
		
		for key, value in record1.items() :
			operation += value
		
		for key, value in record2.items() :
			operation += value
		
		print(" Given (", str1 ,"),(", str2 ,")",sep="")
		#  Display number of remove operation to make anagram
		print(" Result : ", operation)
	

def main() :
	task = Anagrams()
	#    Case A
	#    ------
	#    k m e t e o r o i l , r e m f i l l o t e
	#    ↓             ↓             ↓     ↓
	#    ①            ②             ③    ④
	#    ------------------------------------------------
	#    Remove 4 characters to make both string is anagram
	task.makeAnagram("kmeteoroil", "remfillote")
	#    Case B
	#    ------
	#    C  S I T W I  N  , W  S  O  I  C  N  L  P
	#           ↓   ↓             ↓           ↓  ↓
	#           ①  ②             ③          ④  ⑤
	#    ----------------------------------------------
	#    Remove 5 characters to make both string is anagram
	task.makeAnagram("CSITWIN", "WSOICNLP")
	#    Case C
	#    ------
	#    Both string is anagram
	task.makeAnagram("123", "321")

if __name__ == "__main__": main()

Output

 Given (kmeteoroil),(remfillote)
 Result :  4
 Given (CSITWIN),(WSOICNLP)
 Result :  5
 Given (123),(321)
 Result :  0
#  Ruby program for
#  Minimum number of deletion required to make two strings anagram
class Anagrams 
	def countFrequency(str, record) 
		n = str.length
		i = 0
		#  Calculate frequency of given string
		while (i < n) 
			if (record.key?(str[i])) 
				record[str[i]] = record[str[i]] + 1
			else
				record[str[i]] = 1
			end
			i += 1
		end

	end

	def makeAnagram(str1, str2) 
		operation = 0
		record1 = Hash.new()
		record2 = Hash.new()
		#  Count frequency of given string
		self.countFrequency(str1, record1)
		self.countFrequency(str2, record2)
		k1 = 0
		k2 = 0
		#  Compare same frequency in both string
		record1.each { | key, value |
			if (record2.key?(key)) 
				k1 = record1[key]
				k2 = record2[key]
				if (k1 > k2) 
					operation += k1 - k2
				elsif (k2 > k1) 
					operation += k2 - k1
				end

				#  Reset frequency value
				record1[key] = 0
				record2[key] = 0
			end

		}
		#  Add number of extra characters in str1
		record1.each { | key, value | operation += value}
		#  Add number of extra characters in str2
		record2.each { | key, value | operation += value }
		print(" Given (", str1 ,"),(", str2 ,")", "\n")
		#  Display number of remove operation to make anagram
		print(" Result : ", operation, "\n")
	end

end

def main() 
	task = Anagrams.new()
	#    Case A
	#    ------
	#    k m e t e o r o i l , r e m f i l l o t e
	#    ↓             ↓             ↓     ↓
	#    ①            ②             ③    ④
	#    ------------------------------------------------
	#    Remove 4 characters to make both string is anagram
	task.makeAnagram("kmeteoroil", "remfillote")
	#    Case B
	#    ------
	#    C  S I T W I  N  , W  S  O  I  C  N  L  P
	#           ↓   ↓             ↓           ↓  ↓
	#           ①  ②             ③          ④  ⑤
	#    ----------------------------------------------
	#    Remove 5 characters to make both string is anagram
	task.makeAnagram("CSITWIN", "WSOICNLP")
	#    Case C
	#    ------
	#    Both string is anagram
	task.makeAnagram("123", "321")
end

main()

Output

 Given (kmeteoroil),(remfillote)
 Result : 4
 Given (CSITWIN),(WSOICNLP)
 Result : 5
 Given (123),(321)
 Result : 0
import scala.collection.mutable._;
/*
  Scala program for
  Minimum number of deletion required to make two strings anagram
*/
class Anagrams()
{
	def countFrequency(str: String, 
                       record: Map[Character, Int]): Unit = {
		var n: Int = str.length();
		var i: Int = 0;
		// Calculate frequency of given string
		while (i < n)
		{
			if (record.contains(str.charAt(i)))
			{
				record.addOne(str.charAt(i), 
                              record.get(str.charAt(i)).get + 1);
			}
			else
			{
				record.addOne(str.charAt(i), 1);
			}
			i += 1;
		}
	}
	def makeAnagram(str1: String, str2: String): Unit = {
		var operation: Int = 0;
		var record1: Map[Character, Int] = Map();
		var record2: Map[Character, Int] = Map();
		// Count frequency of given string
		countFrequency(str1, record1);
		countFrequency(str2, record2);
		var k1: Int = 0;
		var k2: Int = 0;
		// Compare same frequency in both string
		
		for ((key,value) <- record1)
 		{
			if (record2.contains(key))
			{
				k1 = value;
				k2 = record2.get(key).get;
				if (k1 > k2)
				{
					operation += k1 - k2;
				}
				else if (k2 > k1)
				{
					operation += k2 - k1;
				}
				// Reset frequency value
				record1.addOne(key, 0);
				record2.addOne(key, 0);
			}
		}
		// Add number of extra characters in str1
		for ((key,value) <- record1)
		{
			operation += value;
		}
		// Add number of extra characters in str2
		for ((key,value) <- record2)
		{
			operation += value;
		}
		println(" Given (" + str1 + "),(" + str2 + ")");
		// Display number of remove operation to make anagram
		println(" Result : " + operation);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Anagrams = new Anagrams();
		/*
		    Case A
		    ------
		    k m e t e o r o i l , r e m f i l l o t e
		    ↓             ↓             ↓     ↓
		    ①            ②             ③    ④
		    ------------------------------------------------
		    Remove 4 characters to make both string is anagram
		*/
		task.makeAnagram("kmeteoroil", "remfillote");
		/*
		    Case B
		    ------
		    C  S I T W I  N  , W  S  O  I  C  N  L  P
		           ↓   ↓             ↓           ↓  ↓
		           ①  ②             ③          ④  ⑤
		    ----------------------------------------------
		    Remove 5 characters to make both string is anagram
		*/
		task.makeAnagram("CSITWIN", "WSOICNLP");
		/*
		    Case C
		    ------
		    Both string is anagram
		*/
		task.makeAnagram("123", "321");
	}
}

Output

 Given (kmeteoroil),(remfillote)
 Result : 4
 Given (CSITWIN),(WSOICNLP)
 Result : 5
 Given (123),(321)
 Result : 0
import Foundation;
/*
  Swift 4 program for
  Minimum number of deletion required to make two strings anagram
*/
class Anagrams
{
	func countFrequency(_ str: [Character], _ record: inout[Character:Int])
	{
		let n: Int = str.count;
		var i: Int = 0;
		// Calculate frequency of given string
		while (i < n)
		{
			if (record.keys.contains(str[i]))
			{
				record[str[i]] = record[str[i]]! + 1;
			}
			else
			{
				record[str[i]] = 1;
			}
			i += 1;
		}
	}
	func makeAnagram(_ str1: String, _ str2: String)
	{
		var operation: Int = 0;
		var record1 = [Character : Int]();
		var record2 = [Character : Int]();
		// Count frequency of given string
		self.countFrequency(Array(str1), &record1);
		self.countFrequency(Array(str2), &record2);
		var k1: Int = 0;
		var k2: Int = 0;
		// Compare same frequency in both string
		for (key, value) in record1
		{
			if (record2.keys.contains(key))
			{
				k1 = value;
				k2 = record2[key]!;
				if (k1 > k2)
				{
					operation += k1 - k2;
				}
				else if (k2 > k1)
				{
					operation += k2 - k1;
				}
				// Reset frequency value
				record1[key] = 0;
				record2[key] = 0;
			}
		}
		// Add number of extra characters in str1
		for (_, value) in record1
		{
			operation += value;
		}
		// Add number of extra characters in str2
		for (_, value) in record2
		{
			operation += value;
		}
		print(" Given (", str1 ,"),(", str2 ,")");
		// Display number of remove operation to make anagram
		print(" Result : ", operation);
	}
}
func main()
{
	let task: Anagrams = Anagrams();
	/*
	    Case A
	    ------
	    k m e t e o r o i l , r e m f i l l o t e
	    ↓             ↓             ↓     ↓
	    ①            ②             ③    ④
	    ------------------------------------------------
	    Remove 4 characters to make both string is anagram
	*/
	task.makeAnagram("kmeteoroil", "remfillote");
	/*
	    Case B
	    ------
	    C  S I T W I  N  , W  S  O  I  C  N  L  P
	           ↓   ↓             ↓           ↓  ↓
	           ①  ②             ③          ④  ⑤
	    ----------------------------------------------
	    Remove 5 characters to make both string is anagram
	*/
	task.makeAnagram("CSITWIN", "WSOICNLP");
	/*
	    Case C
	    ------
	    Both string is anagram
	*/
	task.makeAnagram("123", "321");
}
main();

Output

 Given ( kmeteoroil ),( remfillote )
 Result :  4
 Given ( CSITWIN ),( WSOICNLP )
 Result :  5
 Given ( 123 ),( 321 )
 Result :  0
/*
  Kotlin program for
  Minimum number of deletion required to make two strings anagram
*/
class Anagrams
{
	fun countFrequency(str: String, 
                        record: MutableMap <Char, Int > ): Unit
	{
		val n: Int = str.length;
		var i: Int = 0;
		// Calculate frequency of given string
		while (i < n)
		{
			if (record.containsKey(str.get(i)))
			{
				record.put(str.get(i), record.getValue(str.get(i)) + 1);
			}
			else
			{
				record.put(str.get(i), 1);
			}
			i += 1;
		}
	}
	fun makeAnagram(str1: String, str2: String): Unit
	{
		var operation: Int = 0;
		var record1 = mutableMapOf<Char, Int>();
		var record2 = mutableMapOf<Char, Int>();
		// Count frequency of given string
		this.countFrequency(str1, record1);
		this.countFrequency(str2, record2);
		var k1: Int ;
		var k2: Int ;
		// Compare same frequency in both string
		for ((key, value) in record1)
		{
			if (record2.containsKey(key))
			{
				k1 = value;
				k2 = record2.getValue(key);
				if (k1 > k2)
				{
					operation += k1 - k2;
				}
				else if (k2 > k1)
				{
					operation += k2 - k1;
				}
				// Reset frequency value
				record1.put(key, 0);
				record2.put(key, 0);
			}
		}
		// Add number of extra characters in str1
		for ((_, value) in record1)
		{
			operation += value;
		}
		// Add number of extra characters in str2
		for ((_, value) in record2)
		{
			operation += value;
		}
		println(" Given (" + str1 + "),(" + str2 + ")");
		// Display number of remove operation to make anagram
		println(" Result : " + operation);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Anagrams = Anagrams();
	/*
	    Case A
	    ------
	    k m e t e o r o i l , r e m f i l l o t e
	    ↓             ↓             ↓     ↓
	    ①            ②             ③    ④
	    ------------------------------------------------
	    Remove 4 characters to make both string is anagram
	*/
	task.makeAnagram("kmeteoroil", "remfillote");
	/*
	    Case B
	    ------
	    C  S I T W I  N  , W  S  O  I  C  N  L  P
	           ↓   ↓             ↓           ↓  ↓
	           ①  ②             ③          ④  ⑤
	    ----------------------------------------------
	    Remove 5 characters to make both string is anagram
	*/
	task.makeAnagram("CSITWIN", "WSOICNLP");
	/*
	    Case C
	    ------
	    Both string is anagram
	*/
	task.makeAnagram("123", "321");
}

Output

 Given (kmeteoroil),(remfillote)
 Result : 4
 Given (CSITWIN),(WSOICNLP)
 Result : 5
 Given (123),(321)
 Result : 0




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment