Skip to main content

Transform one string to another using minimum number of operation

Here given code implementation process.

/*
    C program for
    Transform one string to another using minimum number of operation
*/
#include <stdio.h>
#include <string.h>

void countMinOperation(char *str1, char *str2)
{
	int a = strlen(str1);
	int b = strlen(str2);
	int result = 0;
	if (a != b)
	{
		// Not possible to transform two different length string
		result = -1;
	}
	else
	{
		int frequency[256];
		// Set initial frequency in possible characters
		for (int i = 0; i < 256; ++i)
		{
			frequency[i] = 0;
		}
		// Count character frequency of  str1
		for (int i = 0; i < a; ++i)
		{
			frequency[str1[i]]++;
		}
		// Reduce character frequency of str2
		for (int i = 0; i < b; ++i)
		{
			frequency[str2[i]]--;
		}
		// Check the different characters 
		for (int i = 0; i < 256 && result != -1; ++i)
		{
			if (frequency[i] != 0)
			{
				result = -1;
			}
		}
		int i = a - 1;
		int j = i;
		// When result is not -1 and value of [i] is 
        // greater than or equal to zero.
		// Count resultant operation
		while (i >= 0 && result != -1)
		{
			while (i >= 0 && str1[i] != str2[j])
			{
				// Need to changes
				result++;
				i--;
			}
			if (i >= 0)
			{
				j--;
				i--;
			}
		}
	}
	printf("\n %s  %s", str1, str2);
	if (result == -1)
	{
		printf("\n No solution \n");
	}
	else
	{
		printf("\n Number of operations required is : %d", result);
	}
}
int main(int argc, char const *argv[])
{
	// Test 
	/*
	    care  race
	    -----------
	    care <- Get a and insert at beginning
	    
	    acre  (➀ operation)

	    acre <- Get r and insert at beginning
	    
	    race  (➁ operation)
	    --------------------
	    care is transform into race

	    Result = 2

	*/
	countMinOperation("care", "race");
	/*
	    absent  banets
	    --------------
	    absent <- Get t and insert at beginning
	    
	    tabsen  (➀ operation)

	    tabsen <- Get e and insert at beginning
	    
	    etabsn  (➁ operation)

	    etabsn <- Get n and insert at beginning
	    
	    netabs  (➂ operation)

	    netabs <- Get a and insert at beginning
	    
	    anetbs  (➃ operation)

	    anetbs <- Get b and insert at beginning
	    
	    banets  (➄ operation)

	    --------------------
	    absent is transform into a banets

	    Result = 5

	*/
	countMinOperation("absent", "banets");
	// Similar to above approach
	countMinOperation("LISTEN", "SILENT");
	countMinOperation("abc", "abc");
	countMinOperation("abc", "axc");
	return 0;
}

Output

 care  race
 Number of operations required is : 2
 absent  banets
 Number of operations required is : 5
 LISTEN  SILENT
 Number of operations required is : 5
 abc  abc
 Number of operations required is : 0
 abc  axc
 No solution
// Include header file
#include <iostream>
#include <string>

using namespace std;
// C++ program for
//  Transform one string to another using minimum number of operation
class Transformation
{
	public: void countMinOperation(string str1, string str2)
	{
		int a = str1.length();
		int b = str2.length();
		int result = 0;
		if (a != b)
		{
			// Not possible to transform two different length string
			result = -1;
		}
		else
		{
			int frequency[256];
			// Set initial frequency in possible characters
			for (int i = 0; i < 256; ++i)
			{
				frequency[i] = 0;
			}
			// Count character frequency of  str1
			for (int i = 0; i < a; ++i)
			{
				frequency[str1[i]]++;
			}
			// Reduce character frequency of str2
			for (int i = 0; i < b; ++i)
			{
				frequency[str2[i]]--;
			}
			// Check the different characters 
			for (int i = 0; i < 256 && result != -1; ++i)
			{
				if (frequency[i] != 0)
				{
					result = -1;
				}
			}
			int i = a - 1;
			int j = i;
			// When result is not -1 and value of [i] is 
			// greater than or equal to zero.
			// Count resultant operation
			while (i >= 0 && result != -1)
			{
				while (i >= 0 && str1[i] != str2[j])
				{
					// Need to changes
					result++;
					i--;
				}
				if (i >= 0)
				{
					j--;
					i--;
				}
			}
		}
		cout << "\n " << str1 << " " << str2;
		if (result == -1)
		{
			cout << "\n No solution \n";
		}
		else
		{
			cout << "\n Number of operations required is : " << result;
		}
	}
};
int main()
{
	Transformation *task = new Transformation();
	// Test 
	/*
			    care  race
			    -----------
			    care <- Get a and insert at beginning
			    
			    acre  (➀ operation)

			    acre <- Get r and insert at beginning
			    
			    race  (➁ operation)
			    --------------------
			    care is transform into race

			    Result = 2

			*/
	task->countMinOperation("care", "race");
	/*
			    absent  banets
			    --------------
			    absent <- Get t and insert at beginning
			    
			    tabsen  (➀ operation)

			    tabsen <- Get e and insert at beginning
			    
			    etabsn  (➁ operation)

			    etabsn <- Get n and insert at beginning
			    
			    netabs  (➂ operation)

			    netabs <- Get a and insert at beginning
			    
			    anetbs  (➃ operation)

			    anetbs <- Get b and insert at beginning
			    
			    banets  (➄ operation)

			    --------------------
			    absent is transform into a banets

			    Result = 5

			*/
	task->countMinOperation("absent", "banets");
	// Similar to above approach
	task->countMinOperation("LISTEN", "SILENT");
	task->countMinOperation("abc", "abc");
	task->countMinOperation("abc", "axc");
	return 0;
}

Output

 care race
 Number of operations required is : 2
 absent banets
 Number of operations required is : 5
 LISTEN SILENT
 Number of operations required is : 5
 abc abc
 Number of operations required is : 0
 abc axc
 No solution
// Java program for
//  Transform one string to another using minimum number of operation
public class Transformation
{
	public void countMinOperation(String str1, String str2)
	{
		int a = str1.length();
		int b = str2.length();
		int result = 0;
		if (a != b)
		{
			// Not possible to transform two different length string
			result = -1;
		}
		else
		{
			int[] frequency = new int[256];
			// Set initial frequency in possible characters
			for (int i = 0; i < 256; ++i)
			{
				frequency[i] = 0;
			}
			// Count character frequency of  str1
			for (int i = 0; i < a; ++i)
			{
				frequency[str1.charAt(i)]++;
			}
			// Reduce character frequency of str2
			for (int i = 0; i < b; ++i)
			{
				frequency[str2.charAt(i)]--;
			}
			// Check the different characters 
			for (int i = 0; i < 256 && result != -1; ++i)
			{
				if (frequency[i] != 0)
				{
					result = -1;
				}
			}
			int k = a - 1;
			int j = k;
			// When result is not -1 and value of [i] is 
			// greater than or equal to zero.
			// Count resultant operation
			while (k >= 0 && result != -1)
			{
				while (k >= 0 && str1.charAt(k) != str2.charAt(j))
				{
					// Need to changes
					result++;
					k--;
				}
				if (k >= 0)
				{
					j--;
					k--;
				}
			}
		}
		System.out.print("\n " + str1 + " " + str2);
		if (result == -1)
		{
			System.out.print("\n No solution \n");
		}
		else
		{
			System.out.print("\n Number of operations required is : " + result);
		}
	}
	public static void main(String[] args)
	{
		Transformation task = new Transformation();
		// Test 
		/*
		    care  race
		    -----------
		    care <- Get a and insert at beginning
		    
		    acre  (➀ operation)

		    acre <- Get r and insert at beginning
		    
		    race  (➁ operation)
		    --------------------
		    care is transform into race

		    Result = 2

		*/
		task.countMinOperation("care", "race");
		/*
		    absent  banets
		    --------------
		    absent <- Get t and insert at beginning
		    
		    tabsen  (➀ operation)

		    tabsen <- Get e and insert at beginning
		    
		    etabsn  (➁ operation)

		    etabsn <- Get n and insert at beginning
		    
		    netabs  (➂ operation)

		    netabs <- Get a and insert at beginning
		    
		    anetbs  (➃ operation)

		    anetbs <- Get b and insert at beginning
		    
		    banets  (➄ operation)

		    --------------------
		    absent is transform into a banets

		    Result = 5

		*/
		task.countMinOperation("absent", "banets");
		// Similar to above approach
		task.countMinOperation("LISTEN", "SILENT");
		task.countMinOperation("abc", "abc");
		task.countMinOperation("abc", "axc");
	}
}

Output

 care race
 Number of operations required is : 2
 absent banets
 Number of operations required is : 5
 LISTEN SILENT
 Number of operations required is : 5
 abc abc
 Number of operations required is : 0
 abc axc
 No solution
// Include namespace system
using System;
// Csharp program for
//  Transform one string to another using minimum number of operation
public class Transformation
{
	public void countMinOperation(String str1, String str2)
	{
		int a = str1.Length;
		int b = str2.Length;
		int result = 0;
		if (a != b)
		{
			// Not possible to transform two different length string
			result = -1;
		}
		else
		{
			int[] frequency = new int[256];
			// Set initial frequency in possible characters
			for (int i = 0; i < 256; ++i)
			{
				frequency[i] = 0;
			}
			// Count character frequency of  str1
			for (int i = 0; i < a; ++i)
			{
				frequency[str1[i]]++;
			}
			// Reduce character frequency of str2
			for (int i = 0; i < b; ++i)
			{
				frequency[str2[i]]--;
			}
			// Check the different characters
			for (int i = 0; i < 256 && result != -1; ++i)
			{
				if (frequency[i] != 0)
				{
					result = -1;
				}
			}
			int k = a - 1;
			int j = k;
			// When result is not -1 and value of [i] is
			// greater than or equal to zero.
			// Count resultant operation
			while (k >= 0 && result != -1)
			{
				while (k >= 0 && str1[k] != str2[j])
				{
					// Need to changes
					result++;
					k--;
				}
				if (k >= 0)
				{
					j--;
					k--;
				}
			}
		}
		Console.Write("\n " + str1 + " " + str2);
		if (result == -1)
		{
			Console.Write("\n No solution \n");
		}
		else
		{
			Console.Write("\n Number of operations required is : " + result);
		}
	}
	public static void Main(String[] args)
	{
		Transformation task = new Transformation();
		// Test
		/*
		    care  race
		    -----------
		    care <- Get a and insert at beginning
		    
		    acre  (➀ operation)
		    acre <- Get r and insert at beginning
		    
		    race  (➁ operation)
		    --------------------
		    care is transform into race
		    Result = 2
		*/
		task.countMinOperation("care", "race");
		/*
		    absent  banets
		    --------------
		    absent <- Get t and insert at beginning
		    
		    tabsen  (➀ operation)
		    tabsen <- Get e and insert at beginning
		    
		    etabsn  (➁ operation)
		    etabsn <- Get n and insert at beginning
		    
		    netabs  (➂ operation)
		    netabs <- Get a and insert at beginning
		    
		    anetbs  (➃ operation)
		    anetbs <- Get b and insert at beginning
		    
		    banets  (➄ operation)
		    --------------------
		    absent is transform into a banets
		    Result = 5
		*/
		task.countMinOperation("absent", "banets");
		// Similar to above approach
		task.countMinOperation("LISTEN", "SILENT");
		task.countMinOperation("abc", "abc");
		task.countMinOperation("abc", "axc");
	}
}

Output

 care race
 Number of operations required is : 2
 absent banets
 Number of operations required is : 5
 LISTEN SILENT
 Number of operations required is : 5
 abc abc
 Number of operations required is : 0
 abc axc
 No solution
package main

import "fmt"
// Go program for
// Transform one string to another using minimum number of operation

func countMinOperation(str1, str2 string) {
	var a int = len(str1)
	var b int = len(str2)
	var result int = 0
	if a != b {
		// Not possible to transform two different length string
		result = -1
	} else {
		// Set initial frequency in possible characters
		var frequency = make([] int, 256)
		// Count character frequency of  str1
		for i := 0 ; i < a ; i++ {
			frequency[str1[i]]++
		}
		// Reduce character frequency of str2
		for i := 0 ; i < b ; i++ {
			frequency[str2[i]]--
		}
		// Check the different characters
		for i := 0 ; i < 256 && result != -1 ; i++ {
			if frequency[i] != 0 {
				result = -1
			}
		}
		var k int = a - 1
		var j int = k
		// When result is not -1 and value of [i] is
		// greater than or equal to zero.
		// Count resultant operation
		for (k >= 0 && result != -1) {
			for (k >= 0 && str1[k] != str2[j]) {
				// Need to changes
				result++
				k--
			}
			if k >= 0 {
				j--
				k--
			}
		}
	}
	fmt.Print("\n ", str1, " ", str2)
	if result == -1 {
		fmt.Print("\n No solution \n")
	} else {
		fmt.Print("\n Number of operations required is : ", result)
	}
}
func main() {

	// Test
	/*
	    care  race
	    -----------
	    care <- Get a and insert at beginning
	    
	    acre  (➀ operation)
	    acre <- Get r and insert at beginning
	    
	    race  (➁ operation)
	    --------------------
	    care is transform into race
	    Result = 2
	*/
	countMinOperation("care", "race")
	/*
	    absent  banets
	    --------------
	    absent <- Get t and insert at beginning
	    
	    tabsen  (➀ operation)
	    tabsen <- Get e and insert at beginning
	    
	    etabsn  (➁ operation)
	    etabsn <- Get n and insert at beginning
	    
	    netabs  (➂ operation)
	    netabs <- Get a and insert at beginning
	    
	    anetbs  (➃ operation)
	    anetbs <- Get b and insert at beginning
	    
	    banets  (➄ operation)
	    --------------------
	    absent is transform into a banets
	    Result = 5
	*/
	countMinOperation("absent", "banets")
	// Similar to above approach
	countMinOperation("LISTEN", "SILENT")
	countMinOperation("abc", "abc")
	countMinOperation("abc", "axc")
}

Output

 care race
 Number of operations required is : 2
 absent banets
 Number of operations required is : 5
 LISTEN SILENT
 Number of operations required is : 5
 abc abc
 Number of operations required is : 0
 abc axc
 No solution
<?php
// Php program for
// Transform one string to another using minimum number of operation
class Transformation
{
	public	function countMinOperation($str1, $str2)
	{
		$a = strlen($str1);
		$b = strlen($str2);
		$result = 0;
		if ($a != $b)
		{
			// Not possible to transform two different length string
			$result = -1;
		}
		else
		{
			$frequency = array_fill(0, 256, 0);
			// Set initial frequency in possible characters
			for ($i = 0; $i < 256; ++$i)
			{
				$frequency[$i] = 0;
			}
			// Count character frequency of  str1
			for ($i = 0; $i < $a; ++$i)
			{
				$frequency[ord($str1[$i])]++;
			}
			// Reduce character frequency of str2
			for ($i = 0; $i < $b; ++$i)
			{
				$frequency[ord($str2[$i])]--;
			}
			// Check the different characters
			for ($i = 0; $i < 256 && $result != -1; ++$i)
			{
				if ($frequency[$i] != 0)
				{
					$result = -1;
				}
			}
			$k = $a - 1;
			$j = $k;
			// When result is not -1 and value of [i] is
			// greater than or equal to zero.
			// Count resultant operation
			while ($k >= 0 && $result != -1)
			{
				while ($k >= 0 && $str1[$k] != $str2[$j])
				{
					// Need to changes
					$result++;
					$k--;
				}
				if ($k >= 0)
				{
					$j--;
					$k--;
				}
			}
		}
		echo("\n ".$str1." ".$str2);
		if ($result == -1)
		{
			echo("\n No solution \n");
		}
		else
		{
			echo("\n Number of operations required is : ".$result);
		}
	}
}

function main()
{
	$task = new Transformation();
	// Test
	/*
	    care  race
	    -----------
	    care <- Get a and insert at beginning
	    
	    acre  (➀ operation)
	    acre <- Get r and insert at beginning
	    
	    race  (➁ operation)
	    --------------------
	    care is transform into race
	    Result = 2
	*/
	$task->countMinOperation("care", "race");
	/*
	    absent  banets
	    --------------
	    absent <- Get t and insert at beginning
	    
	    tabsen  (➀ operation)
	    tabsen <- Get e and insert at beginning
	    
	    etabsn  (➁ operation)
	    etabsn <- Get n and insert at beginning
	    
	    netabs  (➂ operation)
	    netabs <- Get a and insert at beginning
	    
	    anetbs  (➃ operation)
	    anetbs <- Get b and insert at beginning
	    
	    banets  (➄ operation)
	    --------------------
	    absent is transform into a banets
	    Result = 5
	*/
	$task->countMinOperation("absent", "banets");
	// Similar to above approach
	$task->countMinOperation("LISTEN", "SILENT");
	$task->countMinOperation("abc", "abc");
	$task->countMinOperation("abc", "axc");
}
main();

Output

 care race
 Number of operations required is : 2
 absent banets
 Number of operations required is : 5
 LISTEN SILENT
 Number of operations required is : 5
 abc abc
 Number of operations required is : 0
 abc axc
 No solution
// Node JS program for
//  Transform one string to another using minimum number of operation
class Transformation
{
	countMinOperation(str1, str2)
	{
		var a = str1.length;
		var b = str2.length;
		var result = 0;
		if (a != b)
		{
			// Not possible to transform two different length string
			result = -1;
		}
		else
		{
			// Set initial frequency in possible characters
			var frequency = Array(256).fill(0);
			// Count character frequency of  str1
			for (var i = 0; i < a; ++i)
			{
				frequency[str1.charCodeAt(i)]++;
			}
			// Reduce character frequency of str2
			for (var i = 0; i < b; ++i)
			{
				frequency[str2.charCodeAt(i)]--;
			}
			// Check the different characters
			for (var i = 0; i < 256 && result != -1; ++i)
			{
				if (frequency[i] != 0)
				{
					result = -1;
				}
			}
			var k = a - 1;
			var j = k;
			// When result is not -1 and value of [i] is
			// greater than or equal to zero.
			// Count resultant operation
			while (k >= 0 && result != -1)
			{
				while (k >= 0 && str1.charAt(k) != str2.charAt(j))
				{
					// Need to changes
					result++;
					k--;
				}
				if (k >= 0)
				{
					j--;
					k--;
				}
			}
		}
		process.stdout.write("\n " + str1 + " " + str2);
		if (result == -1)
		{
			process.stdout.write("\n No solution \n");
		}
		else
		{
			process.stdout.write("\n Number of operations required is : " + result);
		}
	}
}

function main()
{
	var task = new Transformation();
	// Test
	/*
	    care  race
	    -----------
	    care <- Get a and insert at beginning
	    
	    acre  (➀ operation)
	    acre <- Get r and insert at beginning
	    
	    race  (➁ operation)
	    --------------------
	    care is transform into race
	    Result = 2
	*/
	task.countMinOperation("care", "race");
	/*
	    absent  banets
	    --------------
	    absent <- Get t and insert at beginning
	    
	    tabsen  (➀ operation)
	    tabsen <- Get e and insert at beginning
	    
	    etabsn  (➁ operation)
	    etabsn <- Get n and insert at beginning
	    
	    netabs  (➂ operation)
	    netabs <- Get a and insert at beginning
	    
	    anetbs  (➃ operation)
	    anetbs <- Get b and insert at beginning
	    
	    banets  (➄ operation)
	    --------------------
	    absent is transform into a banets
	    Result = 5
	*/
	task.countMinOperation("absent", "banets");
	// Similar to above approach
	task.countMinOperation("LISTEN", "SILENT");
	task.countMinOperation("abc", "abc");
	task.countMinOperation("abc", "axc");
}
main();

Output

 care race
 Number of operations required is : 2
 absent banets
 Number of operations required is : 5
 LISTEN SILENT
 Number of operations required is : 5
 abc abc
 Number of operations required is : 0
 abc axc
 No solution
#  Python 3 program for
#   Transform one string to another using minimum number of operation
class Transformation :
	def countMinOperation(self, str1, str2) :
		a = len(str1)
		b = len(str2)
		result = 0
		if (a != b) :
			#  Not possible to transform two different length string
			result = -1
		else :
			#  Set initial frequency in possible characters
			frequency = [0] * (256)
			i = 0
			#  Count character frequency of  str1
			while (i < a) :
				frequency[ord(str1[i])] += 1
				i += 1
			
			i = 0
			#  Reduce character frequency of str2
			while (i < b) :
				frequency[ord(str2[i])] -= 1
				i += 1
			
			i = 0
			#  Check the different characters
			while (i < 256 and result != -1) :
				if (frequency[i] != 0) :
					result = -1
				
				i += 1
			
			k = a - 1
			j = k
			#  When result is not -1 and value of [i] is
			#  greater than or equal to zero.
			#  Count resultant operation
			while (k >= 0 and result != -1) :
				while (k >= 0 and str1[k] != str2[j]) :
					#  Need to changes
					result += 1
					k -= 1
				
				if (k >= 0) :
					j -= 1
					k -= 1
				
			
		
		print("\n ", str1 ," ", str2, end = "")
		if (result == -1) :
			print("\n No solution ")
		else :
			print("\n Number of operations required is : ", result, end = "")
		
	

def main() :
	task = Transformation()
	#  Test
	#    care  race
	#    -----------
	#    care <- Get a and insert at beginning
	#    acre  (➀ operation)
	#    acre <- Get r and insert at beginning
	#    race  (➁ operation)
	#    --------------------
	#    care is transform into race
	#    Result = 2
	task.countMinOperation("care", "race")
	#    absent  banets
	#    --------------
	#    absent <- Get t and insert at beginning
	#    tabsen  (➀ operation)
	#    tabsen <- Get e and insert at beginning
	#    etabsn  (➁ operation)
	#    etabsn <- Get n and insert at beginning
	#    netabs  (➂ operation)
	#    netabs <- Get a and insert at beginning
	#    anetbs  (➃ operation)
	#    anetbs <- Get b and insert at beginning
	#    banets  (➄ operation)
	#    --------------------
	#    absent is transform into a banets
	#    Result = 5
	task.countMinOperation("absent", "banets")
	#  Similar to above approach
	task.countMinOperation("LISTEN", "SILENT")
	task.countMinOperation("abc", "abc")
	task.countMinOperation("abc", "axc")

if __name__ == "__main__": main()

Output

  care   race
 Number of operations required is :  2
  absent   banets
 Number of operations required is :  5
  LISTEN   SILENT
 Number of operations required is :  5
  abc   abc
 Number of operations required is :  0
  abc   axc
 No solution
#  Ruby program for
#   Transform one string to another using minimum number of operation
class Transformation 
	def countMinOperation(str1, str2) 
		a = str1.length
		b = str2.length
		result = 0
		if (a != b) 
			#  Not possible to transform two different length string
			result = -1
		else
 
			#  Set initial frequency in possible characters
			frequency = Array.new(256) {0}
			i = 0
			#  Count character frequency of  str1
			while (i < a) 
				frequency[str1[i].ord] += 1
				i += 1
			end

			i = 0
			#  Reduce character frequency of str2
			while (i < b) 
				frequency[str2[i].ord] -= 1
				i += 1
			end

			i = 0
			#  Check the different characters
			while (i < 256 && result != -1) 
				if (frequency[i] != 0) 
					result = -1
				end

				i += 1
			end

			k = a - 1
			j = k
			#  When result is not -1 and value of [i] is
			#  greater than or equal to zero.
			#  Count resultant operation
			while (k >= 0 && result != -1) 
				while (k >= 0 && str1[k] != str2[j]) 
					#  Need to changes
					result += 1
					k -= 1
				end

				if (k >= 0) 
					j -= 1
					k -= 1
				end

			end

		end

		print("\n ", str1 ," ", str2)
		if (result == -1) 
			print("\n No solution \n")
		else
 
			print("\n Number of operations required is : ", result)
		end

	end

end

def main() 
	task = Transformation.new()
	#  Test
	#    care  race
	#    -----------
	#    care <- Get a and insert at beginning
	#    acre  (➀ operation)
	#    acre <- Get r and insert at beginning
	#    race  (➁ operation)
	#    --------------------
	#    care is transform into race
	#    Result = 2
	task.countMinOperation("care", "race")
	#    absent  banets
	#    --------------
	#    absent <- Get t and insert at beginning
	#    tabsen  (➀ operation)
	#    tabsen <- Get e and insert at beginning
	#    etabsn  (➁ operation)
	#    etabsn <- Get n and insert at beginning
	#    netabs  (➂ operation)
	#    netabs <- Get a and insert at beginning
	#    anetbs  (➃ operation)
	#    anetbs <- Get b and insert at beginning
	#    banets  (➄ operation)
	#    --------------------
	#    absent is transform into a banets
	#    Result = 5
	task.countMinOperation("absent", "banets")
	#  Similar to above approach
	task.countMinOperation("LISTEN", "SILENT")
	task.countMinOperation("abc", "abc")
	task.countMinOperation("abc", "axc")
end

main()

Output

 care race
 Number of operations required is : 2
 absent banets
 Number of operations required is : 5
 LISTEN SILENT
 Number of operations required is : 5
 abc abc
 Number of operations required is : 0
 abc axc
 No solution 
// Scala program for
//  Transform one string to another using minimum number of operation
class Transformation()
{
	def countMinOperation(str1: String, str2: String): Unit = {
		var a: Int = str1.length();
		var b: Int = str2.length();
		var result: Int = 0;
		if (a != b)
		{
			// Not possible to transform two different length string
			result = -1;
		}
		else
		{
			// Set initial frequency in possible characters
			var frequency: Array[Int] = Array.fill[Int](256)(0);
			var i: Int = 0;
			// Count character frequency of  str1
			while (i < a)
			{
				frequency(str1.charAt(i)) += 1;
				i += 1;
			}
			i = 0;
			// Reduce character frequency of str2
			while (i < b)
			{
				frequency(str2.charAt(i)) -= 1;
				i += 1;
			}
			i = 0;
			// Check the different characters
			while (i < 256 && result != -1)
			{
				if (frequency(i) != 0)
				{
					result = -1;
				}
				i += 1;
			}
			var k: Int = a - 1;
			var j: Int = k;
			// When result is not -1 and value of [i] is
			// greater than or equal to zero.
			// Count resultant operation
			while (k >= 0 && result != -1)
			{
				while (k >= 0 && str1.charAt(k) != str2.charAt(j))
				{
					// Need to changes
					result += 1;
					k -= 1;
				}
				if (k >= 0)
				{
					j -= 1;
					k -= 1;
				}
			}
		}
		print("\n " + str1 + " " + str2);
		if (result == -1)
		{
			print("\n No solution \n");
		}
		else
		{
			print("\n Number of operations required is : " + result);
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Transformation = new Transformation();
		// Test
		/*
		    care  race
		    -----------
		    care <- Get a and insert at beginning
		    
		    acre  (➀ operation)
		    acre <- Get r and insert at beginning
		    
		    race  (➁ operation)
		    --------------------
		    care is transform into race
		    Result = 2
		*/
		task.countMinOperation("care", "race");
		/*
		    absent  banets
		    --------------
		    absent <- Get t and insert at beginning
		    
		    tabsen  (➀ operation)
		    tabsen <- Get e and insert at beginning
		    
		    etabsn  (➁ operation)
		    etabsn <- Get n and insert at beginning
		    
		    netabs  (➂ operation)
		    netabs <- Get a and insert at beginning
		    
		    anetbs  (➃ operation)
		    anetbs <- Get b and insert at beginning
		    
		    banets  (➄ operation)
		    --------------------
		    absent is transform into a banets
		    Result = 5
		*/
		task.countMinOperation("absent", "banets");
		// Similar to above approach
		task.countMinOperation("LISTEN", "SILENT");
		task.countMinOperation("abc", "abc");
		task.countMinOperation("abc", "axc");
	}
}

Output

 care race
 Number of operations required is : 2
 absent banets
 Number of operations required is : 5
 LISTEN SILENT
 Number of operations required is : 5
 abc abc
 Number of operations required is : 0
 abc axc
 No solution
import Foundation;
// Swift 4 program for
//  Transform one string to another using minimum number of operation
class Transformation
{
	func countMinOperation(_ text1: String, _ text2: String)
	{
      	let str1 = Array(text1);
      	let str2 = Array(text2);
		let a: Int = str1.count;
		let b: Int = str2.count;
		var result: Int = 0;
		if (a  != b)
		{
			// Not possible to transform two different length string
			result = -1;
		}
		else
		{
			// Set initial frequency in possible characters
			var frequency: [Int] = Array(repeating: 0, count: 256);
			var i: Int = 0;
			// Count character frequency of  str1
			while (i < a)
			{
				frequency[Int(UnicodeScalar(String(str1[i]))!.value)] += 1;
				i += 1;
			}
			i = 0;
			// Reduce character frequency of str2
			while (i < b)
			{
				frequency[Int(UnicodeScalar(String(str2[i]))!.value)] -= 1;
				i += 1;
			}
			i = 0;
			// Check the different characters
			while (i < 256 && result  != -1)
			{
				if (frequency[i]  != 0)
				{
					result = -1;
				}
				i += 1;
			}
			var k: Int = a - 1;
			var j: Int = k;
			// When result is not -1 and value of [i] is
			// greater than or equal to zero.
			// Count resultant operation
			while (k >= 0 && result  != -1)
			{
				while (k >= 0 && str1[k]  != str2[j])
				{
					// Need to changes
					result += 1;
					k -= 1;
				}
				if (k >= 0)
				{
					j -= 1;
					k -= 1;
				}
			}
		}
		print("\n ", text1 ," ", text2, terminator: "");
		if (result == -1)
		{
			print("\n No solution ");
		}
		else
		{
			print("\n Number of operations required is : ", 
                  result, terminator: "");
		}
	}
}
func main()
{
	let task: Transformation = Transformation();
	// Test
	/*
	    care  race
	    -----------
	    care <- Get a and insert at beginning
	    
	    acre  (➀ operation)
	    acre <- Get r and insert at beginning
	    
	    race  (➁ operation)
	    --------------------
	    care is transform into race
	    Result = 2
	*/
	task.countMinOperation("care", "race");
	/*
	    absent  banets
	    --------------
	    absent <- Get t and insert at beginning
	    
	    tabsen  (➀ operation)
	    tabsen <- Get e and insert at beginning
	    
	    etabsn  (➁ operation)
	    etabsn <- Get n and insert at beginning
	    
	    netabs  (➂ operation)
	    netabs <- Get a and insert at beginning
	    
	    anetbs  (➃ operation)
	    anetbs <- Get b and insert at beginning
	    
	    banets  (➄ operation)
	    --------------------
	    absent is transform into a banets
	    Result = 5
	*/
	task.countMinOperation("absent", "banets");
	// Similar to above approach
	task.countMinOperation("LISTEN", "SILENT");
	task.countMinOperation("abc", "abc");
	task.countMinOperation("abc", "axc");
}
main();

Output

  care   race
 Number of operations required is :  2
  absent   banets
 Number of operations required is :  5
  LISTEN   SILENT
 Number of operations required is :  5
  abc   abc
 Number of operations required is :  0
  abc   axc
 No solution
// Kotlin program for
//  Transform one string to another using minimum number of operation
class Transformation
{
	fun countMinOperation(str1: String, str2: String): Unit
	{
		val a: Int = str1.length;
		val b: Int = str2.length;
		var result: Int = 0;
		if (a != b)
		{
			// Not possible to transform two different length string
			result = -1;
		}
		else
		{
			// Set initial frequency in possible characters
			val frequency: Array < Int > = Array(256)
			{
				0
			};
			var i: Int = 0;
			// Count character frequency of  str1
			while (i < a)
			{
				frequency[str1.get(i).toInt()] += 1;
				i += 1;
			}
			i = 0;
			// Reduce character frequency of str2
			while (i < b)
			{
				frequency[str2.get(i).toInt()] -= 1;
				i += 1;
			}
			i = 0;
			// Check the different characters
			while (i < 256 && result != -1)
			{
				if (frequency[i] != 0)
				{
					result = -1;
				}
				i += 1;
			}
			var k: Int = a - 1;
			var j: Int = k;
			// When result is not -1 and value of [i] is
			// greater than or equal to zero.
			// Count resultant operation
			while (k >= 0 && result != -1)
			{
				while (k >= 0 && str1.get(k) != str2.get(j))
				{
					// Need to changes
					result += 1;
					k -= 1;
				}
				if (k >= 0)
				{
					j -= 1;
					k -= 1;
				}
			}
		}
		print("\n " + str1 + " " + str2);
		if (result == -1)
		{
			print("\n No solution \n");
		}
		else
		{
			print("\n Number of operations required is : " + result);
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Transformation = Transformation();
	// Test
	/*
	    care  race
	    -----------
	    care <- Get a and insert at beginning
	    
	    acre  (➀ operation)
	    acre <- Get r and insert at beginning
	    
	    race  (➁ operation)
	    --------------------
	    care is transform into race
	    Result = 2
	*/
	task.countMinOperation("care", "race");
	/*
	    absent  banets
	    --------------
	    absent <- Get t and insert at beginning
	    
	    tabsen  (➀ operation)
	    tabsen <- Get e and insert at beginning
	    
	    etabsn  (➁ operation)
	    etabsn <- Get n and insert at beginning
	    
	    netabs  (➂ operation)
	    netabs <- Get a and insert at beginning
	    
	    anetbs  (➃ operation)
	    anetbs <- Get b and insert at beginning
	    
	    banets  (➄ operation)
	    --------------------
	    absent is transform into a banets
	    Result = 5
	*/
	task.countMinOperation("absent", "banets");
	// Similar to above approach
	task.countMinOperation("LISTEN", "SILENT");
	task.countMinOperation("abc", "abc");
	task.countMinOperation("abc", "axc");
}

Output

 care race
 Number of operations required is : 2
 absent banets
 Number of operations required is : 5
 LISTEN SILENT
 Number of operations required is : 5
 abc abc
 Number of operations required is : 0
 abc axc
 No solution




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