Posted on by Kalkicode
Code String

Interleaving of two given strings with no common characters

Here given code implementation process.

/*
    Java program for
    Interleaving of two given strings with no common characters
*/
public class Interleaving
{
    public void isInterleaving(String a, String b, String text)
    {
        int n = a.length();
        int m = b.length();
        boolean result = true;
        if (n + m != text.length())
        {
            // Case A : 
            // When string combinations length not equal to given text
            result = false;
        }
        else
        {
            int i = 0;
            int j = 0;
            int k = 0;
            // Check characters of A and B is part of text
            while (k < n + m  && result == true)
            {
  
                if (i < n && a.charAt(i) == text.charAt(k))
                {
                    i++;
                }
                else if (j < m && b.charAt(j) == text.charAt(k))
                {
                    j++;
                }
                else
                {
                    result = false;
                }
                           
                k++;
            }
            if (k < n + m)
            {
                // All characters of string A or B not exist in given text
                result = false;
            }

        }
        // Display given strings
        System.out.println(" Given string a : " + a);
        System.out.println(" Given string b : " + b);
        System.out.println(" Given Text  : " + text);
        if (result == true)
        {
            System.out.println(" Yes  ");
        }
        else
        {
            System.out.println(" No   ");
        }
    }
    public static void main(String[] args)
    {
        Interleaving task = new Interleaving();
        // Example A
        //         C o n n e c t i o n <- Given text
        //             n   e   t       <- Given a
        //         C o   n   c   i o n <- Given b
        // ------------------------------------------       
        // 
        // Result = Yes
        task.isInterleaving("net", "Concion", "Connection");
        // Example B
        //        especially <- Given text
        //   (eecia spay) are not produce given text
        // Result = No
        task.isInterleaving("eecia", "spay", "especially");
    }
}

Output

 Given string a : net
 Given string b : Concion
 Given Text  : Connection
 Yes
 Given string a : eecia
 Given string b : spay
 Given Text  : especially
 No
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ program for
    Interleaving of two given strings with no common characters
*/
class Interleaving
{
	public: void isInterleaving(string a, string b, string text)
	{
		int n = a.length();
		int m = b.length();
		bool result = true;
		if (n + m != text.length())
		{
			// Case A : 
			// When string combinations length not equal to given text
			result = false;
		}
		else
		{
			int i = 0;
			int j = 0;
			int k = 0;
			// Check characters of A and B is part of text
			while (k < n + m && result == true)
			{
				if (i < n && a[i] == text[k])
				{
					i++;
				}
				else if (j < m && b[j] == text[k])
				{
					j++;
				}
				else
				{
					result = false;
				}
				k++;
			}
			if (k < n + m)
			{
				// All characters of string A or B not exist in given text
				result = false;
			}
		}
		// Display given strings
		cout << " Given string a : " << a << endl;
		cout << " Given string b : " << b << endl;
		cout << " Given Text  : " << text << endl;
		if (result == true)
		{
			cout << " Yes  " << endl;
		}
		else
		{
			cout << " No   " << endl;
		}
	}
};
int main()
{
	Interleaving *task = new Interleaving();
	// Example A
	//         C o n n e c t i o n <- Given text
	//             n   e   t       <- Given a
	//         C o   n   c   i o n <- Given b
	// ------------------------------------------       
	// 
	// Result = Yes
	task->isInterleaving("net", "Concion", "Connection");
	// Example B
	//        especially <- Given text
	//   (eecia spay) are not produce given text
	// Result = No
	task->isInterleaving("eecia", "spay", "especially");
	return 0;
}

Output

 Given string a : net
 Given string b : Concion
 Given Text  : Connection
 Yes
 Given string a : eecia
 Given string b : spay
 Given Text  : especially
 No
// Include namespace system
using System;
/*
    Csharp program for
    Interleaving of two given strings with no common characters
*/
public class Interleaving
{
	public void isInterleaving(String a, String b, String text)
	{
		int n = a.Length;
		int m = b.Length;
		Boolean result = true;
		if (n + m != text.Length)
		{
			// Case A : 
			// When string combinations length not equal to given text
			result = false;
		}
		else
		{
			int i = 0;
			int j = 0;
			int k = 0;
			// Check characters of A and B is part of text
			while (k < n + m && result == true)
			{
				if (i < n && a[i] == text[k])
				{
					i++;
				}
				else if (j < m && b[j] == text[k])
				{
					j++;
				}
				else
				{
					result = false;
				}
				k++;
			}
			if (k < n + m)
			{
				// All characters of string A or B not exist in given text
				result = false;
			}
		}
		// Display given strings
		Console.WriteLine(" Given string a : " + a);
		Console.WriteLine(" Given string b : " + b);
		Console.WriteLine(" Given Text  : " + text);
		if (result == true)
		{
			Console.WriteLine(" Yes  ");
		}
		else
		{
			Console.WriteLine(" No   ");
		}
	}
	public static void Main(String[] args)
	{
		Interleaving task = new Interleaving();
		// Example A
		//         C o n n e c t i o n <- Given text
		//             n   e   t       <- Given a
		//         C o   n   c   i o n <- Given b
		// ------------------------------------------       
		// 
		// Result = Yes
		task.isInterleaving("net", "Concion", "Connection");
		// Example B
		//        especially <- Given text
		//   (eecia spay) are not produce given text
		// Result = No
		task.isInterleaving("eecia", "spay", "especially");
	}
}

Output

 Given string a : net
 Given string b : Concion
 Given Text  : Connection
 Yes
 Given string a : eecia
 Given string b : spay
 Given Text  : especially
 No
package main
import "fmt"
/*
    Go program for
    Interleaving of two given strings with no common characters
*/

func isInterleaving(a, b, text string) {
	var n int = len(a)
	var m int = len(b)
	var result bool = true
	if n + m != len(text) {
		// Case A : 
		// When string combinations length not equal to given text
		result = false
	} else {
		var i int = 0
		var j int = 0
		var k int = 0
		// Check characters of A and B is part of text
		for (k < n + m && result == true) {
			if i < n && a[i] == text[k] {
				i++
			} else if j < m && b[j] == text[k] {
				j++
			} else {
				result = false
			}
			k++
		}
		if k < n + m {
			// All characters of string A or B not exist in given text
			result = false
		}
	}
	// Display given strings
	fmt.Println(" Given string a : ", a)
	fmt.Println(" Given string b : ", b)
	fmt.Println(" Given Text  : ", text)
	if result == true {
		fmt.Println(" Yes  ")
	} else {
		fmt.Println(" No   ")
	}
}
func main() {

	// Example A
	//         C o n n e c t i o n <- Given text
	//             n   e   t       <- Given a
	//         C o   n   c   i o n <- Given b
	// ------------------------------------------       
	// 
	// Result = Yes
	isInterleaving("net", "Concion", "Connection")
	// Example B
	//        especially <- Given text
	//   (eecia spay) are not produce given text
	// Result = No
	isInterleaving("eecia", "spay", "especially")
}

Output

 Given string a : net
 Given string b : Concion
 Given Text  : Connection
 Yes
 Given string a : eecia
 Given string b : spay
 Given Text  : especially
 No
<?php
/*
    Php program for
    Interleaving of two given strings with no common characters
*/
class Interleaving
{
	public	function isInterleaving($a, $b, $text)
	{
		$n = strlen($a);
		$m = strlen($b);
		$result = true;
		if ($n + $m != strlen($text))
		{
			// Case A : 
			// When string combinations length not equal to given text
			$result = false;
		}
		else
		{
			$i = 0;
			$j = 0;
			$k = 0;
			// Check characters of A and B is part of text
			while ($k < $n + $m && $result == true)
			{
				if ($i < $n && $a[$i] == $text[$k])
				{
					$i++;
				}
				else if ($j < $m && $b[$j] == $text[$k])
				{
					$j++;
				}
				else
				{
					$result = false;
				}
				$k++;
			}
			if ($k < $n + $m)
			{
				// All characters of string A or B not exist in given text
				$result = false;
			}
		}
		// Display given strings
		echo(" Given string a : ".$a.
			"\n");
		echo(" Given string b : ".$b.
			"\n");
		echo(" Given Text  : ".$text.
			"\n");
		if ($result == true)
		{
			echo(" Yes  ".
				"\n");
		}
		else
		{
			echo(" No   ".
				"\n");
		}
	}
}

function main()
{
	$task = new Interleaving();
	// Example A
	//         C o n n e c t i o n <- Given text
	//             n   e   t       <- Given a
	//         C o   n   c   i o n <- Given b
	// ------------------------------------------       
	// 
	// Result = Yes
	$task->isInterleaving("net", "Concion", "Connection");
	// Example B
	//        especially <- Given text
	//   (eecia spay) are not produce given text
	// Result = No
	$task->isInterleaving("eecia", "spay", "especially");
}
main();

Output

 Given string a : net
 Given string b : Concion
 Given Text  : Connection
 Yes
 Given string a : eecia
 Given string b : spay
 Given Text  : especially
 No
/*
    Node JS program for
    Interleaving of two given strings with no common characters
*/
class Interleaving
{
	isInterleaving(a, b, text)
	{
		var n = a.length;
		var m = b.length;
		var result = true;
		if (n + m != text.length)
		{
			// Case A : 
			// When string combinations length not equal to given text
			result = false;
		}
		else
		{
			var i = 0;
			var j = 0;
			var k = 0;
			// Check characters of A and B is part of text
			while (k < n + m && result == true)
			{
				if (i < n && a.charAt(i) == text.charAt(k))
				{
					i++;
				}
				else if (j < m && b.charAt(j) == text.charAt(k))
				{
					j++;
				}
				else
				{
					result = false;
				}
				k++;
			}
			if (k < n + m)
			{
				// All characters of string A or B not exist in given text
				result = false;
			}
		}
		// Display given strings
		console.log(" Given string a : " + a);
		console.log(" Given string b : " + b);
		console.log(" Given Text  : " + text);
		if (result == true)
		{
			console.log(" Yes  ");
		}
		else
		{
			console.log(" No   ");
		}
	}
}

function main()
{
	var task = new Interleaving();
	// Example A
	//         C o n n e c t i o n <- Given text
	//             n   e   t       <- Given a
	//         C o   n   c   i o n <- Given b
	// ------------------------------------------       
	// 
	// Result = Yes
	task.isInterleaving("net", "Concion", "Connection");
	// Example B
	//        especially <- Given text
	//   (eecia spay) are not produce given text
	// Result = No
	task.isInterleaving("eecia", "spay", "especially");
}
main();

Output

 Given string a : net
 Given string b : Concion
 Given Text  : Connection
 Yes
 Given string a : eecia
 Given string b : spay
 Given Text  : especially
 No
#    Python 3 program for
#    Interleaving of two given strings with no common characters
class Interleaving :
	def isInterleaving(self, a, b, text) :
		n = len(a)
		m = len(b)
		result = True
		if (n + m != len(text)) :
			#  Case A : 
			#  When string combinations length not equal to given text
			result = False
		else :
			i = 0
			j = 0
			k = 0
			#  Check characters of A and B is part of text
			while (k < n + m and result == True) :
				if (i < n and a[i] == text[k]) :
					i += 1
				elif (j < m and b[j] == text[k]) :
					j += 1
				else :
					result = False
				
				k += 1
			
			if (k < n + m) :
				#  All characters of string A or B not exist in given text
				result = False
			
		
		#  Display given strings
		print(" Given string a : ", a)
		print(" Given string b : ", b)
		print(" Given Text  : ", text)
		if (result == True) :
			print(" Yes  ")
		else :
			print(" No   ")
		
	

def main() :
	task = Interleaving()
	#  Example A
	#          C o n n e c t i o n <- Given text
	#              n   e   t       <- Given a
	#          C o   n   c   i o n <- Given b
	#  ------------------------------------------       
	#  Result = Yes
	task.isInterleaving("net", "Concion", "Connection")
	#  Example B
	#         especially <- Given text
	#    (eecia spay) are not produce given text
	#  Result = No
	task.isInterleaving("eecia", "spay", "especially")

if __name__ == "__main__": main()

Output

 Given string a :  net
 Given string b :  Concion
 Given Text  :  Connection
 Yes
 Given string a :  eecia
 Given string b :  spay
 Given Text  :  especially
 No
#    Ruby program for
#    Interleaving of two given strings with no common characters
class Interleaving 
	def isInterleaving(a, b, text) 
		n = a.length
		m = b.length
		result = true
		if (n + m != text.length) 
			#  Case A : 
			#  When string combinations length not equal to given text
			result = false
		else
 
			i = 0
			j = 0
			k = 0
			#  Check characters of A and B is part of text
			while (k < n + m && result == true) 
				if (i < n && a[i] == text[k]) 
					i += 1
				elsif (j < m && b[j] == text[k]) 
					j += 1
				else
 
					result = false
				end

				k += 1
			end

			if (k < n + m) 
				#  All characters of string A or B not exist in given text
				result = false
			end

		end

		#  Display given strings
		print(" Given string a : ", a, "\n")
		print(" Given string b : ", b, "\n")
		print(" Given Text  : ", text, "\n")
		if (result == true) 
			print(" Yes  ", "\n")
		else
 
			print(" No   ", "\n")
		end

	end

end

def main() 
	task = Interleaving.new()
	#  Example A
	#          C o n n e c t i o n <- Given text
	#              n   e   t       <- Given a
	#          C o   n   c   i o n <- Given b
	#  ------------------------------------------       
	#  Result = Yes
	task.isInterleaving("net", "Concion", "Connection")
	#  Example B
	#         especially <- Given text
	#    (eecia spay) are not produce given text
	#  Result = No
	task.isInterleaving("eecia", "spay", "especially")
end

main()

Output

 Given string a : net
 Given string b : Concion
 Given Text  : Connection
 Yes  
 Given string a : eecia
 Given string b : spay
 Given Text  : especially
 No   
import scala.collection.mutable._;
/*
    Scala program for
    Interleaving of two given strings with no common characters
*/
class Interleaving()
{
	def isInterleaving(a: String, b: String, text: String): Unit = {
		var n: Int = a.length();
		var m: Int = b.length();
		var result: Boolean = true;
		if (n + m != text.length())
		{
			// Case A : 
			// When string combinations length not equal to given text
			result = false;
		}
		else
		{
			var i: Int = 0;
			var j: Int = 0;
			var k: Int = 0;
			// Check characters of A and B is part of text
			while (k < n + m && result == true)
			{
				if (i < n && a.charAt(i) == text.charAt(k))
				{
					i += 1;
				}
				else if (j < m && b.charAt(j) == text.charAt(k))
				{
					j += 1;
				}
				else
				{
					result = false;
				}
				k += 1;
			}
			if (k < n + m)
			{
				// All characters of string A or B not exist in given text
				result = false;
			}
		}
		// Display given strings
		println(" Given string a : " + a);
		println(" Given string b : " + b);
		println(" Given Text  : " + text);
		if (result == true)
		{
			println(" Yes  ");
		}
		else
		{
			println(" No   ");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Interleaving = new Interleaving();
		// Example A
		//         C o n n e c t i o n <- Given text
		//             n   e   t       <- Given a
		//         C o   n   c   i o n <- Given b
		// ------------------------------------------       
		// 
		// Result = Yes
		task.isInterleaving("net", "Concion", "Connection");
		// Example B
		//        especially <- Given text
		//   (eecia spay) are not produce given text
		// Result = No
		task.isInterleaving("eecia", "spay", "especially");
	}
}

Output

 Given string a : net
 Given string b : Concion
 Given Text  : Connection
 Yes
 Given string a : eecia
 Given string b : spay
 Given Text  : especially
 No
import Foundation;
/*
    Swift 4 program for
    Interleaving of two given strings with no common characters
*/
class Interleaving
{
	func isInterleaving(_ a1: String, _ b1: String, _ text1: String)
	{
      	let a = Array(a1);
      	let b = Array(b1);
      	let text = Array(text1);
		let n: Int = a.count;
		let m: Int = b.count;
		var result: Bool = true;
		if (n + m  != text.count)
		{
			// Case A : 
			// When string combinations length not equal to given text
			result = false;
		}
		else
		{
			var i: Int = 0;
			var j: Int = 0;
			var k: Int = 0;
			// Check characters of A and B is part of text
			while (k < n + m && result == true)
			{
				if (i < n && a[i] == text[k])
				{
					i += 1;
				}
				else if (j < m && b[j] == text[k])
				{
					j += 1;
				}
				else
				{
					result = false;
				}
				k += 1;
			}
			if (k < n + m)
			{
				// All characters of string A or B not exist in given text
				result = false;
			}
		}
		// Display given strings
		print(" Given string a : ", a1);
		print(" Given string b : ", b1);
		print(" Given Text  : ", text1);
		if (result == true)
		{
			print(" Yes  ");
		}
		else
		{
			print(" No   ");
		}
	}
}
func main()
{
	let task: Interleaving = Interleaving();
	// Example A
	//         C o n n e c t i o n <- Given text
	//             n   e   t       <- Given a
	//         C o   n   c   i o n <- Given b
	// ------------------------------------------       
	// 
	// Result = Yes
	task.isInterleaving("net", "Concion", "Connection");
	// Example B
	//        especially <- Given text
	//   (eecia spay) are not produce given text
	// Result = No
	task.isInterleaving("eecia", "spay", "especially");
}
main();

Output

 Given string a :  net
 Given string b :  Concion
 Given Text  :  Connection
 Yes
 Given string a :  eecia
 Given string b :  spay
 Given Text  :  especially
 No
/*
    Kotlin program for
    Interleaving of two given strings with no common characters
*/
class Interleaving
{
	fun isInterleaving(a: String, b: String, text: String): Unit
	{
		val n: Int = a.length;
		val m: Int = b.length;
		var result: Boolean = true;
		if (n + m != text.length)
		{
			// Case A : 
			// When string combinations length not equal to given text
			result = false;
		}
		else
		{
			var i: Int = 0;
			var j: Int = 0;
			var k: Int = 0;
			// Check characters of A and B is part of text
			while (k < n + m && result == true)
			{
				if (i < n && a.get(i) == text.get(k))
				{
					i += 1;
				}
				else if (j < m && b.get(j) == text.get(k))
				{
					j += 1;
				}
				else
				{
					result = false;
				}
				k += 1;
			}
			if (k < n + m)
			{
				// All characters of string A or B not exist in given text
				result = false;
			}
		}
		// Display given strings
		println(" Given string a : " + a);
		println(" Given string b : " + b);
		println(" Given Text  : " + text);
		if (result == true)
		{
			println(" Yes  ");
		}
		else
		{
			println(" No   ");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Interleaving = Interleaving();
	// Example A
	//         C o n n e c t i o n <- Given text
	//             n   e   t       <- Given a
	//         C o   n   c   i o n <- Given b
	// ------------------------------------------       
	// 
	// Result = Yes
	task.isInterleaving("net", "Concion", "Connection");
	// Example B
	//        especially <- Given text
	//   (eecia spay) are not produce given text
	// Result = No
	task.isInterleaving("eecia", "spay", "especially");
}

Output

 Given string a : net
 Given string b : Concion
 Given Text  : Connection
 Yes
 Given string a : eecia
 Given string b : spay
 Given Text  : especially
 No

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