Check if a string is an interleaving of two other strings

Here given code implementation process.

/*
    C program for
    Check if a string is an interleaving of two other strings
*/
#include <stdio.h>
#include <string.h>

void isInterleaving(char *one, char *two, char *text)
{
	// Get length of given strings
	int a = strlen(one) - 1;
	int b = strlen(two) - 1;
	int c = strlen(text) - 1;
	// Result indicator
	int result = 1;
	// Loop Check character sequence from right to left
	while (c >= 0 && result == 1)
	{
		if (a >= 0 && one[a] == text[c])
		{
			// When match with one and text
			a--;
		}
		else if (b >= 0 && two[b] == text[c])
		{
			// When match with two and text
			b--;
		}
		else
		{
			// When order of sequence of string not match
			result = 0;
		}
		c--;
	}
	if (result == 1)
	{
		printf("\n %s is interleaving of %s %s", text, one, two);
	}
	else
	{
		printf("\n %s is not interleaving of %s %s", text, one, two);
	}
}
int main(int argc, char
	const *argv[])
{
	char *one1 = "Cat";
	char *two1 = "hocole";
	char *text1 = "Chocolate";
	//   C     at
	//    hocol  e
	//   Chocolate
	//  -----------
	//      Yes
	isInterleaving(one1, two1, text1);
	char *one2 = "In";
	char *two2 = "crements";
	char *text2 = "Increment";
	//   In    
	//     crements <- s extra
	//   Increment  
	//  -----------
	//      No
	isInterleaving(one2, two2, text2);
	return 0;
}

Output

 Chocolate is interleaving of Cat hocole
 Increment is not interleaving of In crements
/*
    Java program for
    Check if a string is an interleaving of two other strings
*/
public class Interleaving
{
	public void isInterleaving(String one, String two, String text)
	{
		// Get length of given strings
		int a = one.length() - 1;
		int b = two.length() - 1;
		int c = text.length() - 1;
		// Result indicator
		int result = 1;
		// Loop Check character sequence from right to left
		while (c >= 0 && result == 1)
		{
			if (a >= 0 && one.charAt(a) == text.charAt(c))
			{
				// When match with one and text
				a--;
			}
			else if (b >= 0 && two.charAt(b) == text.charAt(c))
			{
				// When match with two and text
				b--;
			}
			else
			{
				// When order of sequence of string not match
				result = 0;
			}
			c--;
		}
		if (result == 1)
		{
			System.out.print("\n " + text + " is interleaving of " + one + " " + two);
		}
		else
		{
			System.out.print("\n " + text + " is not interleaving of " + one + " " + two);
		}
	}
	public static void main(String[] args)
	{
		Interleaving task = new Interleaving();
		String one1 = "Cat";
		String two1 = "hocole";
		String text1 = "Chocolate";
		//   C     at
		//    hocol  e
		//   Chocolate
		//  -----------
		//      Yes
		task.isInterleaving(one1, two1, text1);
		String one2 = "In";
		String two2 = "crements";
		String text2 = "Increment";
		//   In    
		//     crements <- s extra
		//   Increment  
		//  -----------
		//      No
		task.isInterleaving(one2, two2, text2);
	}
}

Output

 Chocolate is interleaving of Cat hocole
 Increment is not interleaving of In crements
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ program for
    Check if a string is an interleaving of two other strings
*/
class Interleaving
{
	public: void isInterleaving(string one, string two, string text)
	{
		// Get length of given strings
		int a = one.length() - 1;
		int b = two.length() - 1;
		int c = text.length() - 1;
		// Result indicator
		int result = 1;
		// Loop Check character sequence from right to left
		while (c >= 0 && result == 1)
		{
			if (a >= 0 && one[a] == text[c])
			{
				// When match with one and text
				a--;
			}
			else if (b >= 0 && two[b] == text[c])
			{
				// When match with two and text
				b--;
			}
			else
			{
				// When order of sequence of string not match
				result = 0;
			}
			c--;
		}
		if (result == 1)
		{
			cout << "\n " << text << " is interleaving of " << one << " " << two;
		}
		else
		{
			cout << "\n " << text << " is not interleaving of " << one << " " << two;
		}
	}
};
int main()
{
	Interleaving *task = new Interleaving();
	string one1 = "Cat";
	string two1 = "hocole";
	string text1 = "Chocolate";
	//   C     at
	//    hocol  e
	//   Chocolate
	//  -----------
	//      Yes
	task->isInterleaving(one1, two1, text1);
	string one2 = "In";
	string two2 = "crements";
	string text2 = "Increment";
	//   In    
	//     crements <- s extra
	//   Increment  
	//  -----------
	//      No
	task->isInterleaving(one2, two2, text2);
	return 0;
}

Output

 Chocolate is interleaving of Cat hocole
 Increment is not interleaving of In crements
// Include namespace system
using System;
/*
    Csharp program for
    Check if a string is an interleaving of two other strings
*/
public class Interleaving
{
	public void isInterleaving(String one, String two, String text)
	{
		// Get length of given strings
		int a = one.Length - 1;
		int b = two.Length - 1;
		int c = text.Length - 1;
		// Result indicator
		int result = 1;
		// Loop Check character sequence from right to left
		while (c >= 0 && result == 1)
		{
			if (a >= 0 && one[a] == text[c])
			{
				// When match with one and text
				a--;
			}
			else if (b >= 0 && two[b] == text[c])
			{
				// When match with two and text
				b--;
			}
			else
			{
				// When order of sequence of string not match
				result = 0;
			}
			c--;
		}
		if (result == 1)
		{
			Console.Write("\n " + text + " is interleaving of " + one + " " + two);
		}
		else
		{
			Console.Write("\n " + text + " is not interleaving of " + one + " " + two);
		}
	}
	public static void Main(String[] args)
	{
		Interleaving task = new Interleaving();
		String one1 = "Cat";
		String two1 = "hocole";
		String text1 = "Chocolate";
		//   C     at
		//    hocol  e
		//   Chocolate
		//  -----------
		//      Yes
		task.isInterleaving(one1, two1, text1);
		String one2 = "In";
		String two2 = "crements";
		String text2 = "Increment";
		//   In    
		//     crements <- s extra
		//   Increment  
		//  -----------
		//      No
		task.isInterleaving(one2, two2, text2);
	}
}

Output

 Chocolate is interleaving of Cat hocole
 Increment is not interleaving of In crements
package main
import "fmt"
/*
    Go program for
    Check if a string is an interleaving of two other strings
*/

func isInterleaving(one, two, text string) {
	// Get length of given strings
	var a int = len(one) - 1
	var b int = len(two) - 1
	var c int = len(text) - 1
	// Result indicator
	var result int = 1
	// Loop Check character sequence from right to left
	for (c >= 0 && result == 1) {
		if a >= 0 && one[a] == text[c] {
			// When match with one and text
			a--
		} else if b >= 0 && two[b] == text[c] {
			// When match with two and text
			b--
		} else {
			// When order of sequence of string not match
			result = 0
		}
		c--
	}
	if result == 1 {
		fmt.Print("\n ", text, " is interleaving of ", one, " ", two)
	} else {
		fmt.Print("\n ", text, " is not interleaving of ", one, " ", two)
	}
}
func main() {
	
	var one1 string = "Cat"
	var two1 string = "hocole"
	var text1 string = "Chocolate"
	//   C     at
	//    hocol  e
	//   Chocolate
	//  -----------
	//      Yes
	isInterleaving(one1, two1, text1)
	var one2 string = "In"
	var two2 string = "crements"
	var text2 string = "Increment"
	//   In    
	//     crements <- s extra
	//   Increment  
	//  -----------
	//      No
	isInterleaving(one2, two2, text2)
}

Output

 Chocolate is interleaving of Cat hocole
 Increment is not interleaving of In crements
<?php
/*
    Php program for
    Check if a string is an interleaving of two other strings
*/
class Interleaving
{
	public	function isInterleaving($one, $two, $text)
	{
		// Get length of given strings
		$a = strlen($one) - 1;
		$b = strlen($two) - 1;
		$c = strlen($text) - 1;
		// Result indicator
		$result = 1;
		// Loop Check character sequence from right to left
		while ($c >= 0 && $result == 1)
		{
			if ($a >= 0 && $one[$a] == $text[$c])
			{
				// When match with one and text
				$a--;
			}
			else if ($b >= 0 && $two[$b] == $text[$c])
			{
				// When match with two and text
				$b--;
			}
			else
			{
				// When order of sequence of string not match
				$result = 0;
			}
			$c--;
		}
		if ($result == 1)
		{
			echo("\n ".$text.
				" is interleaving of ".$one.
				" ".$two);
		}
		else
		{
			echo("\n ".$text.
				" is not interleaving of ".$one.
				" ".$two);
		}
	}
}

function main()
{
	$task = new Interleaving();
	$one1 = "Cat";
	$two1 = "hocole";
	$text1 = "Chocolate";
	//   C     at
	//    hocol  e
	//   Chocolate
	//  -----------
	//      Yes
	$task->isInterleaving($one1, $two1, $text1);
	$one2 = "In";
	$two2 = "crements";
	$text2 = "Increment";
	//   In    
	//     crements <- s extra
	//   Increment  
	//  -----------
	//      No
	$task->isInterleaving($one2, $two2, $text2);
}
main();

Output

 Chocolate is interleaving of Cat hocole
 Increment is not interleaving of In crements
/*
    Node JS program for
    Check if a string is an interleaving of two other strings
*/
class Interleaving
{
	isInterleaving(one, two, text)
	{
		// Get length of given strings
		var a = one.length - 1;
		var b = two.length - 1;
		var c = text.length - 1;
		// Result indicator
		var result = 1;
		// Loop Check character sequence from right to left
		while (c >= 0 && result == 1)
		{
			if (a >= 0 && one.charAt(a) == text.charAt(c))
			{
				// When match with one and text
				a--;
			}
			else if (b >= 0 && two.charAt(b) == text.charAt(c))
			{
				// When match with two and text
				b--;
			}
			else
			{
				// When order of sequence of string not match
				result = 0;
			}
			c--;
		}
		if (result == 1)
		{
			process.stdout.write("\n " + text + " is interleaving of " + one + " " + two);
		}
		else
		{
			process.stdout.write("\n " + text + " is not interleaving of " + one + " " + two);
		}
	}
}

function main()
{
	var task = new Interleaving();
	var one1 = "Cat";
	var two1 = "hocole";
	var text1 = "Chocolate";
	//   C     at
	//    hocol  e
	//   Chocolate
	//  -----------
	//      Yes
	task.isInterleaving(one1, two1, text1);
	var one2 = "In";
	var two2 = "crements";
	var text2 = "Increment";
	//   In    
	//     crements <- s extra
	//   Increment  
	//  -----------
	//      No
	task.isInterleaving(one2, two2, text2);
}
main();

Output

 Chocolate is interleaving of Cat hocole
 Increment is not interleaving of In crements
#    Python 3 program for
#    Check if a string is an interleaving of two other strings
class Interleaving :
	def isInterleaving(self, one, two, text) :
		#  Get length of given strings
		a = len(one) - 1
		b = len(two) - 1
		c = len(text) - 1
		#  Result indicator
		result = 1
		#  Loop Check character sequence from right to left
		while (c >= 0 and result == 1) :
			if (a >= 0 and one[a] == text[c]) :
				#  When match with one and text
				a -= 1
			elif (b >= 0 and two[b] == text[c]) :
				#  When match with two and text
				b -= 1
			else :
				#  When order of sequence of string not match
				result = 0
			
			c -= 1
		
		if (result == 1) :
			print("\n ", text ,"is interleaving of", one , two, end = "")
		else :
			print("\n ", text ,"is not interleaving of", one , two, end = "")
		
	

def main() :
	task = Interleaving()
	one1 = "Cat"
	two1 = "hocole"
	text1 = "Chocolate"
	#    C     at
	#     hocol  e
	#    Chocolate
	#   -----------
	#       Yes
	task.isInterleaving(one1, two1, text1)
	one2 = "In"
	two2 = "crements"
	text2 = "Increment"
	#    In    
	#      crements <- s extra
	#    Increment  
	#   -----------
	#       No
	task.isInterleaving(one2, two2, text2)

if __name__ == "__main__": main()

Output

  Chocolate is interleaving of Cat hocole
  Increment is not interleaving of In crements
#    Ruby program for
#    Check if a string is an interleaving of two other strings
class Interleaving 
	def isInterleaving(one, two, text) 
		#  Get length of given strings
		a = one.length - 1
		b = two.length - 1
		c = text.length - 1
		#  Result indicator
		result = 1
		#  Loop Check character sequence from right to left
		while (c >= 0 && result == 1) 
			if (a >= 0 && one[a] == text[c]) 
				#  When match with one and text
				a -= 1
			elsif (b >= 0 && two[b] == text[c]) 
				#  When match with two and text
				b -= 1
			else
 
				#  When order of sequence of string not match
				result = 0
			end

			c -= 1
		end

		if (result == 1) 
			print("\n ", text ," is interleaving of ", one ," ", two)
		else
 
			print("\n ", text ," is not interleaving of ", one ," ", two)
		end

	end

end

def main() 
	task = Interleaving.new()
	one1 = "Cat"
	two1 = "hocole"
	text1 = "Chocolate"
	#    C     at
	#     hocol  e
	#    Chocolate
	#   -----------
	#       Yes
	task.isInterleaving(one1, two1, text1)
	one2 = "In"
	two2 = "crements"
	text2 = "Increment"
	#    In    
	#      crements <- s extra
	#    Increment  
	#   -----------
	#       No
	task.isInterleaving(one2, two2, text2)
end

main()

Output

 Chocolate is interleaving of Cat hocole
 Increment is not interleaving of In crements
/*
    Scala program for
    Check if a string is an interleaving of two other strings
*/
class Interleaving()
{
	def isInterleaving(one: String, two: String, text: String): Unit = {
		// Get length of given strings
		var a: Int = one.length() - 1;
		var b: Int = two.length() - 1;
		var c: Int = text.length() - 1;
		// Result indicator
		var result: Int = 1;
		// Loop Check character sequence from right to left
		while (c >= 0 && result == 1)
		{
			if (a >= 0 && one.charAt(a) == text.charAt(c))
			{
				// When match with one and text
				a -= 1;
			}
			else if (b >= 0 && two.charAt(b) == text.charAt(c))
			{
				// When match with two and text
				b -= 1;
			}
			else
			{
				// When order of sequence of string not match
				result = 0;
			}
			c -= 1;
		}
		if (result == 1)
		{
			print("\n " + text + " is interleaving of " + one + " " + two);
		}
		else
		{
			print("\n " + text + " is not interleaving of " + one + " " + two);
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Interleaving = new Interleaving();
		var one1: String = "Cat";
		var two1: String = "hocole";
		var text1: String = "Chocolate";
		//   C     at
		//    hocol  e
		//   Chocolate
		//  -----------
		//      Yes
		task.isInterleaving(one1, two1, text1);
		var one2: String = "In";
		var two2: String = "crements";
		var text2: String = "Increment";
		//   In    
		//     crements <- s extra
		//   Increment  
		//  -----------
		//      No
		task.isInterleaving(one2, two2, text2);
	}
}

Output

 Chocolate is interleaving of Cat hocole
 Increment is not interleaving of In crements
import Foundation;
/*
    Swift 4 program for
    Check if a string is an interleaving of two other strings
*/
class Interleaving
{
	func isInterleaving(_ t1: String, _ t2: String, _ t3: String)
	{
      	let one = Array(t1);
      	let two = Array(t2);
      	let text = Array(t3);
		// Get length of given strings
		var a: Int = one.count - 1;
		var b: Int = two.count - 1;
		var c: Int = text.count - 1;
      
		// Result indicator
		var result: Int = 1;
		// Loop Check character sequence from right to left
		while (c >= 0 && result == 1)
		{
			if (a >= 0 && one[a] == text[c])
			{
				// When match with one and text
				a -= 1;
			}
			else if (b >= 0 && two[b] == text[c])
			{
				// When match with two and text
				b -= 1;
			}
			else
			{
				// When order of sequence of string not match
				result = 0;
			}
			c -= 1;
		}
		if (result == 1)
		{
			print("\n ", t3 ," is interleaving of", t1 , t2, terminator: "");
		}
		else
		{
			print("\n ", t3 ,"is not interleaving of", t1 , t2, terminator: "");
		}
	}
}
func main()
{
	let task: Interleaving = Interleaving();
	let one1: String = "Cat";
	let two1: String = "hocole";
	let text1: String = "Chocolate";
	//   C     at
	//    hocol  e
	//   Chocolate
	//  -----------
	//      Yes
	task.isInterleaving(one1, two1, text1);
	let one2: String = "In";
	let two2: String = "crements";
	let text2: String = "Increment";
	//   In    
	//     crements <- s extra
	//   Increment  
	//  -----------
	//      No
	task.isInterleaving(one2, two2, text2);
}
main();

Output

  Chocolate  is interleaving of Cat hocole
  Increment is not interleaving of In crements
/*
    Kotlin program for
    Check if a string is an interleaving of two other strings
*/
class Interleaving
{
	fun isInterleaving(one: String, two: String, text: String): Unit
	{
		// Get length of given strings
		var a: Int = one.length - 1;
		var b: Int = two.length - 1;
		var c: Int = text.length - 1;
		// Result indicator
		var result: Int = 1;
		// Loop Check character sequence from right to left
		while (c >= 0 && result == 1)
		{
			if (a >= 0 && one.get(a) == text.get(c))
			{
				// When match with one and text
				a -= 1;
			}
			else if (b >= 0 && two.get(b) == text.get(c))
			{
				// When match with two and text
				b -= 1;
			}
			else
			{
				// When order of sequence of string not match
				result = 0;
			}
			c -= 1;
		}
		if (result == 1)
		{
			print("\n " + text + " is interleaving of " + one + " " + two);
		}
		else
		{
			print("\n " + text + " is not interleaving of " + one + " " + two);
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Interleaving = Interleaving();
	val one1: String = "Cat";
	val two1: String = "hocole";
	val text1: String = "Chocolate";
	//   C     at
	//    hocol  e
	//   Chocolate
	//  -----------
	//      Yes
	task.isInterleaving(one1, two1, text1);
	val one2: String = "In";
	val two2: String = "crements";
	val text2: String = "Increment";
	//   In    
	//     crements <- s extra
	//   Increment  
	//  -----------
	//      No
	task.isInterleaving(one2, two2, text2);
}

Output

 Chocolate is interleaving of Cat hocole
 Increment is not interleaving of In crements

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







© 2021, kalkicode.com, All rights reserved