Generate combination of two equal length string which is belong to index

Here given code implementation process.

// C Program
// Generate combination of two equal length string which is belong to index 
#include <stdio.h>
#include <string.h>

// Display result
void printSequence(char result[], int n)
{
	for (int i = 0; i < n; ++i)
	{
		printf("  %c", result[i]);
	}
	printf("\n");
}
void findCombination(char *text1, char *text2, 
                     char result[], int index, int n)
{
	if (index == n)
	{
		// Display calculated result
		printSequence(result, index);
		return;
	}
	if (index >= n)
	{
		// Base case when stop process
		return;
	}
	// Collects resultant value of text1
	result[index] = text1[index];
	findCombination(text1, text2, result, index + 1, n);
	// Collects resultant value of text2
	result[index] = text2[index];
	findCombination(text1, text2, result, index + 1, n);
}
void combination(char *text1, char *text2)
{
	if (strlen(text1) != strlen(text2))
	{
		// When string are not equal to each other
		return;
	}
	int n = strlen(text1);
	printf("\n Given Text1 : %s Text2 : %s \n", text1, text2);
	// Collect result
	char result[n];
	// Test
	findCombination(text1, text2, result, 0, n);
}
int main()
{
	combination("XYZ", "ABC");
	combination("abcd", "efgh");
	return 0;
}

Output

 Given Text1 : XYZ Text2 : ABC
  X  Y  Z
  X  Y  C
  X  B  Z
  X  B  C
  A  Y  Z
  A  Y  C
  A  B  Z
  A  B  C

 Given Text1 : abcd Text2 : efgh
  a  b  c  d
  a  b  c  h
  a  b  g  d
  a  b  g  h
  a  f  c  d
  a  f  c  h
  a  f  g  d
  a  f  g  h
  e  b  c  d
  e  b  c  h
  e  b  g  d
  e  b  g  h
  e  f  c  d
  e  f  c  h
  e  f  g  d
  e  f  g  h
// Java Program 
// Generate combination of two equal length string which is belong to index 
public class Combinations
{
	public void findCombination(String text1, 
                                 String text2, 
                                 String result, 
                                 int index, 
                                 int n)
	{
		if (index == n)
		{
			System.out.print("\n " + result);
			return;
		}
		if (index >= n)
		{
			// Base case when stop process
			return;
		}
      	// Find next solution using recursion
		findCombination(text1, text2, 
                        result + text1.charAt(index), 
                        index + 1, n);
		findCombination(text1, text2, 
                        result + text2.charAt(index), 
                        index + 1, n);
	}
	public void combination(String text1, String text2)
	{
		if (text1.length() != text2.length())
		{
			// When string are not equal to each other
			return;
		}
		int n = text1.length();
		System.out.print("\n Given Text1 : " + text1 + " Text2 : " + text2);
		// Test
		findCombination(text1, text2, "", 0, n);
	}
	public static void main(String args[])
	{
		Combinations task = new Combinations();
		// Test
		task.combination("XYZ", "ABC");
		task.combination("abcd", "efgh");
	}
}

Output

 Given Text1 : XYZ Text2 : ABC
 XYZ
 XYC
 XBZ
 XBC
 AYZ
 AYC
 ABZ
 ABC
 Given Text1 : abcd Text2 : efgh
 abcd
 abch
 abgd
 abgh
 afcd
 afch
 afgd
 afgh
 ebcd
 ebch
 ebgd
 ebgh
 efcd
 efch
 efgd
 efgh
// Include header file
#include <iostream>
#include <string>

using namespace std;
// C++ Program 
// Generate combination of two equal length string which is belong to index 
class Combinations
{
	public: void findCombination(string text1, 
                                 string text2, 
                                 string result, 
                                 int index, 
                                 int n)
	{
		if (index == n)
		{
			cout << "\n " << result;
			return;
		}
		if (index >= n)
		{
			// Base case when stop process
			return;
		}
		// Find next solution using recursion
		this->findCombination(text1, text2, 
                              result  +  (text1[index]), 
                              index + 1, n);
		this->findCombination(text1, text2, 
                              result  +  (text2[index]),
                              index + 1, n);
	}
	void combination(string text1, string text2)
	{
		if (text1.length() != text2.length())
		{
			// When string are not equal to each other
			return;
		}
		int n = text1.length();
		cout << "\n Given Text1 : " << text1 << " Text2 : " << text2;
		// Test
		this->findCombination(text1, text2, "", 0, n);
	}
};
int main()
{
	Combinations *task = new Combinations();
	// Test
	task->combination("XYZ", "ABC");
	task->combination("abcd", "efgh");
	return 0;
}

Output

 Given Text1 : XYZ Text2 : ABC
 XYZ
 XYC
 XBZ
 XBC
 AYZ
 AYC
 ABZ
 ABC
 Given Text1 : abcd Text2 : efgh
 abcd
 abch
 abgd
 abgh
 afcd
 afch
 afgd
 afgh
 ebcd
 ebch
 ebgd
 ebgh
 efcd
 efch
 efgd
 efgh
// Include namespace system
using System;
// Csharp Program 
// Generate combination of two equal length string which is belong to index 
public class Combinations
{
	public void findCombination(String text1, 
                                 String text2, String result, 
                                 int index, int n)
	{
		if (index == n)
		{
			Console.Write("\n " + result);
			return;
		}
		if (index >= n)
		{
			// Base case when stop process
			return;
		}
		// Find next solution using recursion
		this.findCombination(text1, text2, 
                             result + text1[index], index + 1, n);
		this.findCombination(text1, text2, 
                             result + text2[index], index + 1, n);
	}
	public void combination(String text1, String text2)
	{
		if (text1.Length != text2.Length)
		{
			// When string are not equal to each other
			return;
		}
		int n = text1.Length;
		Console.Write("\n Given Text1 : " + text1 + " Text2 : " + text2);
		// Test
		this.findCombination(text1, text2, "", 0, n);
	}
	public static void Main(String[] args)
	{
		Combinations task = new Combinations();
		// Test
		task.combination("XYZ", "ABC");
		task.combination("abcd", "efgh");
	}
}

Output

 Given Text1 : XYZ Text2 : ABC
 XYZ
 XYC
 XBZ
 XBC
 AYZ
 AYC
 ABZ
 ABC
 Given Text1 : abcd Text2 : efgh
 abcd
 abch
 abgd
 abgh
 afcd
 afch
 afgd
 afgh
 ebcd
 ebch
 ebgd
 ebgh
 efcd
 efch
 efgd
 efgh
package main
import "fmt"
// Go Program 
// Generate combination of two equal length string which is belong to index 
type Combinations struct {}
func getCombinations() * Combinations {
	var me *Combinations = &Combinations {}
	return me
}
func(this Combinations) findCombination(text1 string, 
	text2 string, result string, index int, n int) {
	if index == n {
		fmt.Print("\n ", result)
		return
	}
	if index >= n {
		// Base case when stop process
		return
	}
	// Find next solution using recursion
	this.findCombination(text1, text2, 
		result + string(text1[index]), index + 1, n)
	this.findCombination(text1, text2, 
		result + string(text2[index]), index + 1, n)
}
func(this Combinations) combination(text1, text2 string) {
	if len(text1) != len(text2) {
		// When string are not equal to each other
		return
	}
	var n int = len(text1)
	fmt.Print("\n Given Text1 : ", text1, " Text2 : ", text2)
	// Test
	this.findCombination(text1, text2, "", 0, n)
}
func main() {
	var task * Combinations = getCombinations()
	// Test
	task.combination("XYZ", "ABC")
	task.combination("abcd", "efgh")
}

Output

 Given Text1 : XYZ Text2 : ABC
 XYZ
 XYC
 XBZ
 XBC
 AYZ
 AYC
 ABZ
 ABC
 Given Text1 : abcd Text2 : efgh
 abcd
 abch
 abgd
 abgh
 afcd
 afch
 afgd
 afgh
 ebcd
 ebch
 ebgd
 ebgh
 efcd
 efch
 efgd
 efgh
<?php
// Php Program 
// Generate combination of two equal length string which is belong to index 
class Combinations
{
	public	function findCombination($text1, 
                                      $text2, 
                                      $result, 
                                      $index, 
                                      $n)
	{
		if ($index == $n)
		{
			echo("\n ".$result);
			return;
		}
		if ($index >= $n)
		{
			// Base case when stop process
			return;
		}
		// Find next solution using recursion
		$this->findCombination($text1, $text2, 
                               $result.strval($text1[$index]), 
                               $index + 1, $n);
		$this->findCombination($text1, $text2, 
                               $result.strval($text2[$index]), 
                               $index + 1, $n);
	}
	public	function combination($text1, $text2)
	{
		if (strlen($text1) != strlen($text2))
		{
			// When string are not equal to each other
			return;
		}
		$n = strlen($text1);
		echo("\n Given Text1 : ".$text1.
			" Text2 : ".$text2);
		// Test
		$this->findCombination($text1, $text2, "", 0, $n);
	}
}

function main()
{
	$task = new Combinations();
	// Test
	$task->combination("XYZ", "ABC");
	$task->combination("abcd", "efgh");
}
main();

Output

 Given Text1 : XYZ Text2 : ABC
 XYZ
 XYC
 XBZ
 XBC
 AYZ
 AYC
 ABZ
 ABC
 Given Text1 : abcd Text2 : efgh
 abcd
 abch
 abgd
 abgh
 afcd
 afch
 afgd
 afgh
 ebcd
 ebch
 ebgd
 ebgh
 efcd
 efch
 efgd
 efgh
// Node JS Program 
// Generate combination of two equal length string which is belong to index 
class Combinations
{
	findCombination(text1, 
                     text2, 
                     result, 
                     index, 
                     n)
	{
		if (index == n)
		{
			process.stdout.write("\n " + result);
			return;
		}
		if (index >= n)
		{
			// Base case when stop process
			return;
		}
		// Find next solution using recursion
		this.findCombination(text1, text2, 
                             result + text1.charAt(index), index + 1, n);
		this.findCombination(text1, text2, 
                             result + text2.charAt(index), index + 1, n);
	}
	combination(text1, text2)
	{
		if (text1.length != text2.length)
		{
			// When string are not equal to each other
			return;
		}
		var n = text1.length;
		process.stdout.write("\n Given Text1 : " + 
                             text1 + " Text2 : " + text2);
		// Test
		this.findCombination(text1, text2, "", 0, n);
	}
}

function main()
{
	var task = new Combinations();
	// Test
	task.combination("XYZ", "ABC");
	task.combination("abcd", "efgh");
}
main();

Output

 Given Text1 : XYZ Text2 : ABC
 XYZ
 XYC
 XBZ
 XBC
 AYZ
 AYC
 ABZ
 ABC
 Given Text1 : abcd Text2 : efgh
 abcd
 abch
 abgd
 abgh
 afcd
 afch
 afgd
 afgh
 ebcd
 ebch
 ebgd
 ebgh
 efcd
 efch
 efgd
 efgh
#  Python 3 Program 
#  Generate combination of two equal length string which is belong to index 
class Combinations :
	def findCombination(self, text1, 
                        text2, 
                        result, 
                        index, n) :
		if (index == n) :
			print("\n ", result, end = "")
			return
		
		if (index >= n) :
			#  Base case when stop process
			return
		
		#  Find next solution using recursion
		self.findCombination(text1, text2, 
                             result + str(text1[index]), index + 1, n)
		self.findCombination(text1, text2, 
                             result + str(text2[index]), index + 1, n)
	
	def combination(self, text1, text2) :
		if (len(text1) != len(text2)) :
			#  When string are not equal to each other
			return
		
		n = len(text1)
		print("\n Given Text1 : ", text1 ,
              " Text2 : ", text2, end = "")
		#  Test
		self.findCombination(text1, text2, "", 0, n)
	

def main() :
	task = Combinations()
	#  Test
	task.combination("XYZ", "ABC")
	task.combination("abcd", "efgh")

if __name__ == "__main__": main()

Output

 Given Text1 :  XYZ  Text2 :  ABC
  XYZ
  XYC
  XBZ
  XBC
  AYZ
  AYC
  ABZ
  ABC
 Given Text1 :  abcd  Text2 :  efgh
  abcd
  abch
  abgd
  abgh
  afcd
  afch
  afgd
  afgh
  ebcd
  ebch
  ebgd
  ebgh
  efcd
  efch
  efgd
  efgh
#  Ruby Program 
#  Generate combination of two equal length string which is belong to index 
class Combinations 
	def findCombination(text1, text2, result, index, n) 
		if (index == n) 
			print("\n ", result)
			return
		end

		if (index >= n) 
			#  Base case when stop process
			return
		end

		#  Find next solution using recursion
		self.findCombination(text1, text2, 
                             result + text1[index].to_s, index + 1, n)
		self.findCombination(text1, text2, 
                             result + text2[index].to_s, index + 1, n)
	end

	def combination(text1, text2) 
		if (text1.length != text2.length) 
			#  When string are not equal to each other
			return
		end

		n = text1.length
		print("\n Given Text1 : ", text1 ," Text2 : ", text2)
		#  Test
		self.findCombination(text1, text2, "", 0, n)
	end

end

def main() 
	task = Combinations.new()
	#  Test
	task.combination("XYZ", "ABC")
	task.combination("abcd", "efgh")
end

main()

Output

 Given Text1 : XYZ Text2 : ABC
 XYZ
 XYC
 XBZ
 XBC
 AYZ
 AYC
 ABZ
 ABC
 Given Text1 : abcd Text2 : efgh
 abcd
 abch
 abgd
 abgh
 afcd
 afch
 afgd
 afgh
 ebcd
 ebch
 ebgd
 ebgh
 efcd
 efch
 efgd
 efgh
import scala.collection.mutable._;
// Scala Program 
// Generate combination of two equal length string which is belong to index 
class Combinations()
{
	def findCombination(text1: String, text2: String, 
                        result: String, index: Int, n: Int): Unit = {
		if (index == n)
		{
			print("\n " + result);
			return;
		}
		if (index >= n)
		{
			// Base case when stop process
			return;
		}
		// Find next solution using recursion
		findCombination(text1, text2, 
                        result + text1.charAt(index).toString(), index + 1, n);
		findCombination(text1, text2, 
                        result + text2.charAt(index).toString(), index + 1, n);
	}
	def combination(text1: String, text2: String): Unit = {
		if (text1.length() != text2.length())
		{
			// When string are not equal to each other
			return;
		}
		var n: Int = text1.length();
		print("\n Given Text1 : " + text1 + " Text2 : " + text2);
		// Test
		findCombination(text1, text2, "", 0, n);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Combinations = new Combinations();
		// Test
		task.combination("XYZ", "ABC");
		task.combination("abcd", "efgh");
	}
}

Output

 Given Text1 : XYZ Text2 : ABC
 XYZ
 XYC
 XBZ
 XBC
 AYZ
 AYC
 ABZ
 ABC
 Given Text1 : abcd Text2 : efgh
 abcd
 abch
 abgd
 abgh
 afcd
 afch
 afgd
 afgh
 ebcd
 ebch
 ebgd
 ebgh
 efcd
 efch
 efgd
 efgh
import Foundation;
// Swift 4 Program 
// Generate combination of two equal length string which is belong to index 
class Combinations
{
	func findCombination(_ text1: [Character], 
                          _ text2: [Character], 
                          _ result: String, 
                          _ index: Int, 
                          _ n: Int)
	{
		if (index == n)
		{
			print("\n ", result, terminator: "");
			return;
		}
		if (index >= n)
		{
			// Base case when stop process
			return;
		}
		// Find next solution using recursion
		self.findCombination(text1, text2, 
                             result + String(text1[index]), index + 1, n);
		self.findCombination(text1, text2, 
                             result + String(text2[index]), index + 1, n);
	}
	func combination(_ text1: String, _ text2: String)
	{
		if (text1.count  != text2.count)
		{
			// When string are not equal to each other
			return;
		}
		let n: Int = text1.count;
		print("\n Given Text1 : ", text1 ,
              " Text2 : ", text2, terminator: "");
		// Test
		self.findCombination(Array(text1), Array(text2), "", 0, n);
	}
}
func main()
{
	let task: Combinations = Combinations();
	// Test
	task.combination("XYZ", "ABC");
	task.combination("abcd", "efgh");
}
main();

Output

 Given Text1 :  XYZ  Text2 :  ABC
  XYZ
  XYC
  XBZ
  XBC
  AYZ
  AYC
  ABZ
  ABC
 Given Text1 :  abcd  Text2 :  efgh
  abcd
  abch
  abgd
  abgh
  afcd
  afch
  afgd
  afgh
  ebcd
  ebch
  ebgd
  ebgh
  efcd
  efch
  efgd
  efgh
// Kotlin Program 
// Generate combination of two equal length string which is belong to index 
class Combinations
{
	fun findCombination(text1: String, 
                         text2: String, 
                         result: String, 
                         index: Int, 
                         n: Int): Unit
	{
		if (index == n)
		{
			print("\n " + result);
			return;
		}
		if (index >= n)
		{
			// Base case when stop process
			return;
		}
		// Find next solution using recursion
		this.findCombination(text1, 
                             text2, result + text1.get(index).toString(), 
                             index + 1, n);
		this.findCombination(text1, 
                             text2, result + text2.get(index).toString(), 
                             index + 1, n);
	}
	fun combination(text1: String, text2: String): Unit
	{
		if (text1.length != text2.length)
		{
			// When string are not equal to each other
			return;
		}
		val n: Int = text1.length;
		print("\n Given Text1 : " + text1 + " Text2 : " + text2);
		// Test
		this.findCombination(text1, text2, "", 0, n);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Combinations = Combinations();
	// Test
	task.combination("XYZ", "ABC");
	task.combination("abcd", "efgh");
}

Output

 Given Text1 : XYZ Text2 : ABC
 XYZ
 XYC
 XBZ
 XBC
 AYZ
 AYC
 ABZ
 ABC
 Given Text1 : abcd Text2 : efgh
 abcd
 abch
 abgd
 abgh
 afcd
 afch
 afgd
 afgh
 ebcd
 ebch
 ebgd
 ebgh
 efcd
 efch
 efgd
 efgh


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