Posted on by Kalkicode
Code Backtracking

Print all combinations of other digits on a '$' place of given sequence

Here given code implementation process.

//  C program for
//  Print all combinations of other digits on a '$' place of given sequence
#include <stdio.h>
#include <string.h>

// Display result
void printSequence(int result[], int n)
{
	for (int i = 0; i < n; ++i)
	{
		printf("  %d", result[i]);
	}
	printf("\n");
}
void findCombination(char * num, int digit[], 
  					int result[], int index, int n)
{
	if (index == n)
	{
		printSequence(result, index);
		return;
	}
	if (num[index] == '$')
	{
		for (int i = 0; i <= 9; ++i)
		{
			if (digit[i] == 1)
			{
				result[index] = i;
				findCombination(num, digit, 
                                result, index + 1, n);
			}
		}
	}
	else
	{
		result[index] = num[index] - '0';
		findCombination(num, digit, result, index + 1, n);
	}
}
void combination(char * num)
{
	int n = strlen(num);
	if (n < 0)
	{
		return;
	}
	printf("\n Given String : %s\n", num);
	int result[n];
	int digit[10];
	// Set initial frequency
	for (int i = 0; i < 9; ++i)
	{
		digit[i] = 0;
	}
	// Find other digit
	for (int i = 0; i < n; ++i)
	{
		if (num[i] != '$')
		{
			// Active digit status
			digit[num[i] - '0'] = 1;
		}
	}
	findCombination(num, digit, result, 0, n);
}
int main(int argc, char
	const * argv[])
{
	// Text : 1$2$3
	// ------------------
	//   1  1  2  1  3
	//   1  1  2  2  3
	//   1  1  2  3  3
	//   1  2  2  1  3
	//   1  2  2  2  3
	//   1  2  2  3  3
	//   1  3  2  1  3
	//   1  3  2  2  3
	//   1  3  2  3  3
	combination("1$2$3");
	// Text : $$$861
	//   1  1  1  8  6  1
	//   1  1  6  8  6  1
	//   1  1  8  8  6  1
	//   1  6  1  8  6  1
	//   1  6  6  8  6  1
	//   1  6  8  8  6  1
	//   1  8  1  8  6  1
	//   1  8  6  8  6  1
	//   1  8  8  8  6  1
	//   6  1  1  8  6  1
	//   6  1  6  8  6  1
	//   6  1  8  8  6  1
	//   6  6  1  8  6  1
	//   6  6  6  8  6  1
	//   6  6  8  8  6  1
	//   6  8  1  8  6  1
	//   6  8  6  8  6  1
	//   6  8  8  8  6  1
	//   8  1  1  8  6  1
	//   8  1  6  8  6  1
	//   8  1  8  8  6  1
	//   8  6  1  8  6  1
	//   8  6  6  8  6  1
	//   8  6  8  8  6  1
	//   8  8  1  8  6  1
	//   8  8  6  8  6  1
	//   8  8  8  8  6  1
	combination("$$$861");
	return 0;
}

Output

 Given String : 1$2$3
  1  1  2  1  3
  1  1  2  2  3
  1  1  2  3  3
  1  2  2  1  3
  1  2  2  2  3
  1  2  2  3  3
  1  3  2  1  3
  1  3  2  2  3
  1  3  2  3  3

 Given String : $$$861
  1  1  1  8  6  1
  1  1  6  8  6  1
  1  1  8  8  6  1
  1  6  1  8  6  1
  1  6  6  8  6  1
  1  6  8  8  6  1
  1  8  1  8  6  1
  1  8  6  8  6  1
  1  8  8  8  6  1
  6  1  1  8  6  1
  6  1  6  8  6  1
  6  1  8  8  6  1
  6  6  1  8  6  1
  6  6  6  8  6  1
  6  6  8  8  6  1
  6  8  1  8  6  1
  6  8  6  8  6  1
  6  8  8  8  6  1
  8  1  1  8  6  1
  8  1  6  8  6  1
  8  1  8  8  6  1
  8  6  1  8  6  1
  8  6  6  8  6  1
  8  6  8  8  6  1
  8  8  1  8  6  1
  8  8  6  8  6  1
  8  8  8  8  6  1
// Java Program 
// Print all combinations of other digits on a '$' place of given sequence
public class Sequence
{
	public void findCombination(String num, 
                                boolean[] digit, String result, 
  								int index, 
  								int n)
	{
		if (index == n)
		{
			System.out.println(result);
			return;
		}
		if (num.charAt(index) == '$')
		{
			for (int i = 0; i <= 9; ++i)
			{
				if (digit[i] == true)
				{
					findCombination(num, digit, 
                                    result + " " + i, 
                                    index + 1, n);
				}
			}
		}
		else
		{
			findCombination(num, digit, 
                            result + " " + num.charAt(index), index + 1, n);
		}
	}
	public void combination(String num)
	{
		int n = num.length();
		if (n < 0)
		{
			return;
		}
		System.out.println("\n Given String : " + num);
		boolean[] digit = new boolean[10];
		// Set initial frequency
		for (int i = 0; i < 9; ++i)
		{
			digit[i] = false;
		}
		// Find other digit
		for (int i = 0; i < n; ++i)
		{
			if (num.charAt(i) != '$')
			{
				// Active digit status
				digit[num.charAt(i) - '0'] = true;
			}
		}
		findCombination(num, digit, "", 0, n);
	}
	public static void main(String[] args)
	{
		Sequence task = new Sequence();
		// Text : 1$2$3
		// ------------------
		//   1  1  2  1  3
		//   1  1  2  2  3
		//   1  1  2  3  3
		//   1  2  2  1  3
		//   1  2  2  2  3
		//   1  2  2  3  3
		//   1  3  2  1  3
		//   1  3  2  2  3
		//   1  3  2  3  3
		task.combination("1$2$3");
		// Text : $$$861
		//   1  1  1  8  6  1
		//   1  1  6  8  6  1
		//   1  1  8  8  6  1
		//   1  6  1  8  6  1
		//   1  6  6  8  6  1
		//   1  6  8  8  6  1
		//   1  8  1  8  6  1
		//   1  8  6  8  6  1
		//   1  8  8  8  6  1
		//   6  1  1  8  6  1
		//   6  1  6  8  6  1
		//   6  1  8  8  6  1
		//   6  6  1  8  6  1
		//   6  6  6  8  6  1
		//   6  6  8  8  6  1
		//   6  8  1  8  6  1
		//   6  8  6  8  6  1
		//   6  8  8  8  6  1
		//   8  1  1  8  6  1
		//   8  1  6  8  6  1
		//   8  1  8  8  6  1
		//   8  6  1  8  6  1
		//   8  6  6  8  6  1
		//   8  6  8  8  6  1
		//   8  8  1  8  6  1
		//   8  8  6  8  6  1
		//   8  8  8  8  6  1
		task.combination("$$$861");
	}
}

Output

 Given String : 1$2$3
 1 1 2 1 3
 1 1 2 2 3
 1 1 2 3 3
 1 2 2 1 3
 1 2 2 2 3
 1 2 2 3 3
 1 3 2 1 3
 1 3 2 2 3
 1 3 2 3 3

 Given String : $$$861
 1 1 1 8 6 1
 1 1 6 8 6 1
 1 1 8 8 6 1
 1 6 1 8 6 1
 1 6 6 8 6 1
 1 6 8 8 6 1
 1 8 1 8 6 1
 1 8 6 8 6 1
 1 8 8 8 6 1
 6 1 1 8 6 1
 6 1 6 8 6 1
 6 1 8 8 6 1
 6 6 1 8 6 1
 6 6 6 8 6 1
 6 6 8 8 6 1
 6 8 1 8 6 1
 6 8 6 8 6 1
 6 8 8 8 6 1
 8 1 1 8 6 1
 8 1 6 8 6 1
 8 1 8 8 6 1
 8 6 1 8 6 1
 8 6 6 8 6 1
 8 6 8 8 6 1
 8 8 1 8 6 1
 8 8 6 8 6 1
 8 8 8 8 6 1
// Include header file
#include <iostream>
#include <string>
using namespace std;
// C++ Program 
// Print all combinations of other digits on a '$' place of given sequence
class Sequence
{
	public: void findCombination(string num, 
                                 bool digit[], 
      string result, 
        int index, 
          int n)
	{
		if (index == n)
		{
			cout << result << endl;
			return;
		}
		if (num[index] == '$')
		{
			for (int i = 0; i <= 9; ++i)
			{
				if (digit[i] == true)
				{
					this->findCombination(num, digit, 
                    result  +  " " +  to_string(i), index + 1, n);
				}
			}
		}
		else
		{
			this->findCombination(num, digit, 
              result  +  " " +  num[index], index + 1, n);
		}
	}
	void combination(string num)
	{
		int n = num.length();
		if (n < 0)
		{
			return;
		}
		cout << "\n Given String : " << num << endl;
		bool digit[10];
		// Set initial frequency
		for (int i = 0; i < 9; ++i)
		{
			digit[i] = false;
		}
		// Find other digit
		for (int i = 0; i < n; ++i)
		{
			if (num[i] != '$')
			{
				// Active digit status
				digit[num[i] - '0'] = true;
			}
		}
		this->findCombination(num, digit, "", 0, n);
	}
};
int main()
{
	Sequence *task = new Sequence();
	// Text : 1$2$3
	// ------------------
	//   1  1  2  1  3
	//   1  1  2  2  3
	//   1  1  2  3  3
	//   1  2  2  1  3
	//   1  2  2  2  3
	//   1  2  2  3  3
	//   1  3  2  1  3
	//   1  3  2  2  3
	//   1  3  2  3  3
	task->combination("1$2$3");
	// Text : $$$861
	//   1  1  1  8  6  1
	//   1  1  6  8  6  1
	//   1  1  8  8  6  1
	//   1  6  1  8  6  1
	//   1  6  6  8  6  1
	//   1  6  8  8  6  1
	//   1  8  1  8  6  1
	//   1  8  6  8  6  1
	//   1  8  8  8  6  1
	//   6  1  1  8  6  1
	//   6  1  6  8  6  1
	//   6  1  8  8  6  1
	//   6  6  1  8  6  1
	//   6  6  6  8  6  1
	//   6  6  8  8  6  1
	//   6  8  1  8  6  1
	//   6  8  6  8  6  1
	//   6  8  8  8  6  1
	//   8  1  1  8  6  1
	//   8  1  6  8  6  1
	//   8  1  8  8  6  1
	//   8  6  1  8  6  1
	//   8  6  6  8  6  1
	//   8  6  8  8  6  1
	//   8  8  1  8  6  1
	//   8  8  6  8  6  1
	//   8  8  8  8  6  1
	task->combination("$$$861");
	return 0;
}

Output

 Given String : 1$2$3
 1 1 2 1 3
 1 1 2 2 3
 1 1 2 3 3
 1 2 2 1 3
 1 2 2 2 3
 1 2 2 3 3
 1 3 2 1 3
 1 3 2 2 3
 1 3 2 3 3

 Given String : $$$861
 1 1 1 8 6 1
 1 1 6 8 6 1
 1 1 8 8 6 1
 1 6 1 8 6 1
 1 6 6 8 6 1
 1 6 8 8 6 1
 1 8 1 8 6 1
 1 8 6 8 6 1
 1 8 8 8 6 1
 6 1 1 8 6 1
 6 1 6 8 6 1
 6 1 8 8 6 1
 6 6 1 8 6 1
 6 6 6 8 6 1
 6 6 8 8 6 1
 6 8 1 8 6 1
 6 8 6 8 6 1
 6 8 8 8 6 1
 8 1 1 8 6 1
 8 1 6 8 6 1
 8 1 8 8 6 1
 8 6 1 8 6 1
 8 6 6 8 6 1
 8 6 8 8 6 1
 8 8 1 8 6 1
 8 8 6 8 6 1
 8 8 8 8 6 1
// Include namespace system
using System;
// Csharp Program 
// Print all combinations of other digits on a '$' place of given sequence
public class Sequence
{
	public void findCombination(String num, 
                                Boolean[] digit, 
  								String result, 
  								int index, int n)
	{
		if (index == n)
		{
			Console.WriteLine(result);
			return;
		}
		if (num[index] == '$')
		{
			for (int i = 0; i <= 9; ++i)
			{
				if (digit[i] == true)
				{
					this.findCombination(num, 
                                         digit, 
                                         result + " " + i, 
                                         index + 1, 
                                         n);
				}
			}
		}
		else
		{
			this.findCombination(num, digit, 
                                 result + " " + num[index], 
                                 index + 1, n);
		}
	}
	public void combination(String num)
	{
		int n = num.Length;
		if (n < 0)
		{
			return;
		}
		Console.WriteLine("\n Given String : " + num);
		Boolean[] digit = new Boolean[10];
		// Set initial frequency
		for (int i = 0; i < 9; ++i)
		{
			digit[i] = false;
		}
		// Find other digit
		for (int i = 0; i < n; ++i)
		{
			if (num[i] != '$')
			{
				// Active digit status
				digit[num[i] - '0'] = true;
			}
		}
		this.findCombination(num, digit, "", 0, n);
	}
	public static void Main(String[] args)
	{
		Sequence task = new Sequence();
		// Text : 1$2$3
		// ------------------
		//   1  1  2  1  3
		//   1  1  2  2  3
		//   1  1  2  3  3
		//   1  2  2  1  3
		//   1  2  2  2  3
		//   1  2  2  3  3
		//   1  3  2  1  3
		//   1  3  2  2  3
		//   1  3  2  3  3
		task.combination("1$2$3");
		// Text : $$$861
		//   1  1  1  8  6  1
		//   1  1  6  8  6  1
		//   1  1  8  8  6  1
		//   1  6  1  8  6  1
		//   1  6  6  8  6  1
		//   1  6  8  8  6  1
		//   1  8  1  8  6  1
		//   1  8  6  8  6  1
		//   1  8  8  8  6  1
		//   6  1  1  8  6  1
		//   6  1  6  8  6  1
		//   6  1  8  8  6  1
		//   6  6  1  8  6  1
		//   6  6  6  8  6  1
		//   6  6  8  8  6  1
		//   6  8  1  8  6  1
		//   6  8  6  8  6  1
		//   6  8  8  8  6  1
		//   8  1  1  8  6  1
		//   8  1  6  8  6  1
		//   8  1  8  8  6  1
		//   8  6  1  8  6  1
		//   8  6  6  8  6  1
		//   8  6  8  8  6  1
		//   8  8  1  8  6  1
		//   8  8  6  8  6  1
		//   8  8  8  8  6  1
		task.combination("$$$861");
	}
}

Output

 Given String : 1$2$3
 1 1 2 1 3
 1 1 2 2 3
 1 1 2 3 3
 1 2 2 1 3
 1 2 2 2 3
 1 2 2 3 3
 1 3 2 1 3
 1 3 2 2 3
 1 3 2 3 3

 Given String : $$$861
 1 1 1 8 6 1
 1 1 6 8 6 1
 1 1 8 8 6 1
 1 6 1 8 6 1
 1 6 6 8 6 1
 1 6 8 8 6 1
 1 8 1 8 6 1
 1 8 6 8 6 1
 1 8 8 8 6 1
 6 1 1 8 6 1
 6 1 6 8 6 1
 6 1 8 8 6 1
 6 6 1 8 6 1
 6 6 6 8 6 1
 6 6 8 8 6 1
 6 8 1 8 6 1
 6 8 6 8 6 1
 6 8 8 8 6 1
 8 1 1 8 6 1
 8 1 6 8 6 1
 8 1 8 8 6 1
 8 6 1 8 6 1
 8 6 6 8 6 1
 8 6 8 8 6 1
 8 8 1 8 6 1
 8 8 6 8 6 1
 8 8 8 8 6 1
package main

import "strconv"
import "fmt"
// Go Program 
// Print all combinations of other digits on a '$' place of given sequence
type Sequence struct {}
func getSequence() * Sequence {
	var me *Sequence = &Sequence {}
	return me
}
func(this Sequence) findCombination(num string, 
	digit[] bool, 
	result string, 
	index int, 
	n int) {
	if index == n {
		fmt.Println(result)
		return
	}
	if num[index] == '$' {
		for i := 0 ; i <= 9 ; i++ {
			if digit[i] == true {
				this.findCombination(num, digit, 
					result + " "+ strconv.Itoa(i), 
					index + 1, n)
			}
		}
	} else {
		this.findCombination(num, digit, 
			result + " " + string(num[index]), index + 1, n)
	}
}
func(this Sequence) combination(num string) {
	var n int = len(num)
	if n < 0 {
		return
	}
	fmt.Println("\n Given String : ", num)
	var digit = make([] bool, 10)
	// Set initial frequency
	for i := 0 ; i < 9 ; i++ {
		digit[i] = false
	}
	// Find other digit
	for i := 0 ; i < n ; i++ {
		if num[i] != '$' {
			// Active digit status
			digit[num[i] - '0'] = true
		}
	}
	this.findCombination(num, digit, "", 0, n)
}
func main() {
	var task * Sequence = getSequence()
	// Text : 1$2$3
	// ------------------
	//   1  1  2  1  3
	//   1  1  2  2  3
	//   1  1  2  3  3
	//   1  2  2  1  3
	//   1  2  2  2  3
	//   1  2  2  3  3
	//   1  3  2  1  3
	//   1  3  2  2  3
	//   1  3  2  3  3
	task.combination("1$2$3")
	// Text : $$$861
	//   1  1  1  8  6  1
	//   1  1  6  8  6  1
	//   1  1  8  8  6  1
	//   1  6  1  8  6  1
	//   1  6  6  8  6  1
	//   1  6  8  8  6  1
	//   1  8  1  8  6  1
	//   1  8  6  8  6  1
	//   1  8  8  8  6  1
	//   6  1  1  8  6  1
	//   6  1  6  8  6  1
	//   6  1  8  8  6  1
	//   6  6  1  8  6  1
	//   6  6  6  8  6  1
	//   6  6  8  8  6  1
	//   6  8  1  8  6  1
	//   6  8  6  8  6  1
	//   6  8  8  8  6  1
	//   8  1  1  8  6  1
	//   8  1  6  8  6  1
	//   8  1  8  8  6  1
	//   8  6  1  8  6  1
	//   8  6  6  8  6  1
	//   8  6  8  8  6  1
	//   8  8  1  8  6  1
	//   8  8  6  8  6  1
	//   8  8  8  8  6  1
	task.combination("$$$861")
}

Output

 Given String : 1$2$3
 1 1 2 1 3
 1 1 2 2 3
 1 1 2 3 3
 1 2 2 1 3
 1 2 2 2 3
 1 2 2 3 3
 1 3 2 1 3
 1 3 2 2 3
 1 3 2 3 3

 Given String : $$$861
 1 1 1 8 6 1
 1 1 6 8 6 1
 1 1 8 8 6 1
 1 6 1 8 6 1
 1 6 6 8 6 1
 1 6 8 8 6 1
 1 8 1 8 6 1
 1 8 6 8 6 1
 1 8 8 8 6 1
 6 1 1 8 6 1
 6 1 6 8 6 1
 6 1 8 8 6 1
 6 6 1 8 6 1
 6 6 6 8 6 1
 6 6 8 8 6 1
 6 8 1 8 6 1
 6 8 6 8 6 1
 6 8 8 8 6 1
 8 1 1 8 6 1
 8 1 6 8 6 1
 8 1 8 8 6 1
 8 6 1 8 6 1
 8 6 6 8 6 1
 8 6 8 8 6 1
 8 8 1 8 6 1
 8 8 6 8 6 1
 8 8 8 8 6 1
<?php
// Php Program 
// Print all combinations of other digits on a '$' place of given sequence
class Sequence
{
	public	function findCombination($num, $digit, $result, $index, $n)
	{
		if ($index == $n)
		{
			echo($result."\n");
			return;
		}
		if ($num[$index] == '$')
		{
			for ($i = 0; $i <= 9; ++$i)
			{
				if ($digit[$i] == true)
				{
					$this->findCombination($num, $digit, 
                    $result." ".strval($i), $index + 1, $n);
				}
			}
		}
		else
		{
			$this->findCombination($num, $digit, 
            $result." ".strval($num[$index]), $index + 1, $n);
		}
	}
	public	function combination($num)
	{
		$n = strlen($num);
		if ($n < 0)
		{
			return;
		}
		echo("\n Given String : ".$num."\n");
		$digit = array_fill(0, 10, false);
		// Find other digit
		for ($i = 0; $i < $n; ++$i)
		{
			if ($num[$i] != '$')
			{
				// Active digit status
				$digit[ord($num[$i]) - ord('0')] = true;
			}
		}
		$this->findCombination($num, $digit, "", 0, $n);
	}
}

function main()
{
	$task = new Sequence();
	// Text : 1$2$3
	// ------------------
	//   1  1  2  1  3
	//   1  1  2  2  3
	//   1  1  2  3  3
	//   1  2  2  1  3
	//   1  2  2  2  3
	//   1  2  2  3  3
	//   1  3  2  1  3
	//   1  3  2  2  3
	//   1  3  2  3  3
	$task->combination("1$2$3");
	// Text : $$$861
	//   1  1  1  8  6  1
	//   1  1  6  8  6  1
	//   1  1  8  8  6  1
	//   1  6  1  8  6  1
	//   1  6  6  8  6  1
	//   1  6  8  8  6  1
	//   1  8  1  8  6  1
	//   1  8  6  8  6  1
	//   1  8  8  8  6  1
	//   6  1  1  8  6  1
	//   6  1  6  8  6  1
	//   6  1  8  8  6  1
	//   6  6  1  8  6  1
	//   6  6  6  8  6  1
	//   6  6  8  8  6  1
	//   6  8  1  8  6  1
	//   6  8  6  8  6  1
	//   6  8  8  8  6  1
	//   8  1  1  8  6  1
	//   8  1  6  8  6  1
	//   8  1  8  8  6  1
	//   8  6  1  8  6  1
	//   8  6  6  8  6  1
	//   8  6  8  8  6  1
	//   8  8  1  8  6  1
	//   8  8  6  8  6  1
	//   8  8  8  8  6  1
	$task->combination("$$$861");
}
main();

Output

 Given String : 1$2$3
 1 1 2 1 3
 1 1 2 2 3
 1 1 2 3 3
 1 2 2 1 3
 1 2 2 2 3
 1 2 2 3 3
 1 3 2 1 3
 1 3 2 2 3
 1 3 2 3 3

 Given String : $$$861
 1 1 1 8 6 1
 1 1 6 8 6 1
 1 1 8 8 6 1
 1 6 1 8 6 1
 1 6 6 8 6 1
 1 6 8 8 6 1
 1 8 1 8 6 1
 1 8 6 8 6 1
 1 8 8 8 6 1
 6 1 1 8 6 1
 6 1 6 8 6 1
 6 1 8 8 6 1
 6 6 1 8 6 1
 6 6 6 8 6 1
 6 6 8 8 6 1
 6 8 1 8 6 1
 6 8 6 8 6 1
 6 8 8 8 6 1
 8 1 1 8 6 1
 8 1 6 8 6 1
 8 1 8 8 6 1
 8 6 1 8 6 1
 8 6 6 8 6 1
 8 6 8 8 6 1
 8 8 1 8 6 1
 8 8 6 8 6 1
 8 8 8 8 6 1
// Node JS Program 
// Print all combinations of other digits on a '$' place of given sequence
class Sequence
{
	findCombination(num, digit, result, index, n)
	{
		if (index == n)
		{
			console.log(result);
			return;
		}
		if (num.charAt(index) == '$')
		{
			for (var i = 0; i <= 9; ++i)
			{
				if (digit[i] == true)
				{
					this.findCombination(num, digit, 
                                         result + " " + i, 
                                         index + 1, n);
				}
			}
		}
		else
		{
			this.findCombination(num, digit, 
                                 result + " " + num.charAt(index), 
                                 index + 1, n);
		}
	}
	combination(num)
	{
		var n = num.length;
		if (n < 0)
		{
			return;
		}
		console.log("\n Given String : " + num);
		var digit = Array(10).fill(false);
		// Find other digit
		for (var i = 0; i < n; ++i)
		{
			if (num.charAt(i) != '$')
			{
				// Active digit status
				digit[num.charCodeAt(i) - 48] = true;
			}
		}
		this.findCombination(num, digit, "", 0, n);
	}
}

function main()
{
	var task = new Sequence();
	// Text : 1$2$3
	// ------------------
	//   1  1  2  1  3
	//   1  1  2  2  3
	//   1  1  2  3  3
	//   1  2  2  1  3
	//   1  2  2  2  3
	//   1  2  2  3  3
	//   1  3  2  1  3
	//   1  3  2  2  3
	//   1  3  2  3  3
	task.combination("1$2$3");
	// Text : $$$861
	//   1  1  1  8  6  1
	//   1  1  6  8  6  1
	//   1  1  8  8  6  1
	//   1  6  1  8  6  1
	//   1  6  6  8  6  1
	//   1  6  8  8  6  1
	//   1  8  1  8  6  1
	//   1  8  6  8  6  1
	//   1  8  8  8  6  1
	//   6  1  1  8  6  1
	//   6  1  6  8  6  1
	//   6  1  8  8  6  1
	//   6  6  1  8  6  1
	//   6  6  6  8  6  1
	//   6  6  8  8  6  1
	//   6  8  1  8  6  1
	//   6  8  6  8  6  1
	//   6  8  8  8  6  1
	//   8  1  1  8  6  1
	//   8  1  6  8  6  1
	//   8  1  8  8  6  1
	//   8  6  1  8  6  1
	//   8  6  6  8  6  1
	//   8  6  8  8  6  1
	//   8  8  1  8  6  1
	//   8  8  6  8  6  1
	//   8  8  8  8  6  1
	task.combination("$$$861");
}
main();

Output

 Given String : 1$2$3
 1 1 2 1 3
 1 1 2 2 3
 1 1 2 3 3
 1 2 2 1 3
 1 2 2 2 3
 1 2 2 3 3
 1 3 2 1 3
 1 3 2 2 3
 1 3 2 3 3

 Given String : $$$861
 1 1 1 8 6 1
 1 1 6 8 6 1
 1 1 8 8 6 1
 1 6 1 8 6 1
 1 6 6 8 6 1
 1 6 8 8 6 1
 1 8 1 8 6 1
 1 8 6 8 6 1
 1 8 8 8 6 1
 6 1 1 8 6 1
 6 1 6 8 6 1
 6 1 8 8 6 1
 6 6 1 8 6 1
 6 6 6 8 6 1
 6 6 8 8 6 1
 6 8 1 8 6 1
 6 8 6 8 6 1
 6 8 8 8 6 1
 8 1 1 8 6 1
 8 1 6 8 6 1
 8 1 8 8 6 1
 8 6 1 8 6 1
 8 6 6 8 6 1
 8 6 8 8 6 1
 8 8 1 8 6 1
 8 8 6 8 6 1
 8 8 8 8 6 1
#  Python 3 Program 
#  Print all combinations of other digits on a '$' place of given sequence
class Sequence :
	def findCombination(self, num, digit, result, index, n) :
		if (index == n) :
			print(result)
			return
		
		if (num[index] == '$') :
			i = 0
			while (i <= 9) :
				if (digit[i] == True) :
					self.findCombination(num, 
                                         digit, 
                                         result + " " + str(i), 
                                         index + 1, n)
				
				i += 1
			
		else :
			self.findCombination(num, digit, 
                                 result + " " + str(num[index]), 
                                 index + 1, n)
		
	
	def combination(self, num) :
		n = len(num)
		if (n < 0) :
			return
		
		print("\n Given String : ", num)
		digit = [False] * (10)
		i = 0
		#  Find other digit
		while (i < n) :
			if (num[i] != '$') :
				#  Active digit status
				digit[ord(num[i]) - ord('0')] = True
			
			i += 1
		
		self.findCombination(num, digit, "", 0, n)
	

def main() :
	task = Sequence()
	#  Text : 1$2$3
	#  ------------------
	#    1  1  2  1  3
	#    1  1  2  2  3
	#    1  1  2  3  3
	#    1  2  2  1  3
	#    1  2  2  2  3
	#    1  2  2  3  3
	#    1  3  2  1  3
	#    1  3  2  2  3
	#    1  3  2  3  3
	task.combination("1$2$3")
	#  Text : $$$861
	#    1  1  1  8  6  1
	#    1  1  6  8  6  1
	#    1  1  8  8  6  1
	#    1  6  1  8  6  1
	#    1  6  6  8  6  1
	#    1  6  8  8  6  1
	#    1  8  1  8  6  1
	#    1  8  6  8  6  1
	#    1  8  8  8  6  1
	#    6  1  1  8  6  1
	#    6  1  6  8  6  1
	#    6  1  8  8  6  1
	#    6  6  1  8  6  1
	#    6  6  6  8  6  1
	#    6  6  8  8  6  1
	#    6  8  1  8  6  1
	#    6  8  6  8  6  1
	#    6  8  8  8  6  1
	#    8  1  1  8  6  1
	#    8  1  6  8  6  1
	#    8  1  8  8  6  1
	#    8  6  1  8  6  1
	#    8  6  6  8  6  1
	#    8  6  8  8  6  1
	#    8  8  1  8  6  1
	#    8  8  6  8  6  1
	#    8  8  8  8  6  1
	task.combination("$$$861")

if __name__ == "__main__": main()

Output

 Given String :  1$2$3
 1 1 2 1 3
 1 1 2 2 3
 1 1 2 3 3
 1 2 2 1 3
 1 2 2 2 3
 1 2 2 3 3
 1 3 2 1 3
 1 3 2 2 3
 1 3 2 3 3

 Given String :  $$$861
 1 1 1 8 6 1
 1 1 6 8 6 1
 1 1 8 8 6 1
 1 6 1 8 6 1
 1 6 6 8 6 1
 1 6 8 8 6 1
 1 8 1 8 6 1
 1 8 6 8 6 1
 1 8 8 8 6 1
 6 1 1 8 6 1
 6 1 6 8 6 1
 6 1 8 8 6 1
 6 6 1 8 6 1
 6 6 6 8 6 1
 6 6 8 8 6 1
 6 8 1 8 6 1
 6 8 6 8 6 1
 6 8 8 8 6 1
 8 1 1 8 6 1
 8 1 6 8 6 1
 8 1 8 8 6 1
 8 6 1 8 6 1
 8 6 6 8 6 1
 8 6 8 8 6 1
 8 8 1 8 6 1
 8 8 6 8 6 1
 8 8 8 8 6 1
#  Ruby Program 
#  Print all combinations of other digits on a '$' place of given sequence
class Sequence 
	def findCombination(num, digit, result, index, n) 
		if (index == n) 
			print(result, "\n")
			return
		end

		if (num[index] == '$') 
			i = 0
			while (i <= 9) 
				if (digit[i] == true) 
					self.findCombination(num, digit, 
                    result + " " + i.to_s, index + 1, n)
				end

				i += 1
			end

		else
 
			self.findCombination(num, digit, 
            result + " "+ num[index].to_s, index + 1, n)
		end

	end

	def combination(num) 
		n = num.length
		if (n < 0) 
			return
		end

		print("\n Given String : ", num, "\n")
		digit = Array.new(10) {false}
		i = 0
		#  Find other digit
		while (i < n) 
			if (num[i] != '$') 
				#  Active digit status
				digit[num[i].ord - '0'.ord] = true
			end

			i += 1
		end

		self.findCombination(num, digit, "", 0, n)
	end

end

def main() 
	task = Sequence.new()
	#  Text : 1$2$3
	#  ------------------
	#    1  1  2  1  3
	#    1  1  2  2  3
	#    1  1  2  3  3
	#    1  2  2  1  3
	#    1  2  2  2  3
	#    1  2  2  3  3
	#    1  3  2  1  3
	#    1  3  2  2  3
	#    1  3  2  3  3
	task.combination("1$2$3")
	#  Text : $$$861
	#    1  1  1  8  6  1
	#    1  1  6  8  6  1
	#    1  1  8  8  6  1
	#    1  6  1  8  6  1
	#    1  6  6  8  6  1
	#    1  6  8  8  6  1
	#    1  8  1  8  6  1
	#    1  8  6  8  6  1
	#    1  8  8  8  6  1
	#    6  1  1  8  6  1
	#    6  1  6  8  6  1
	#    6  1  8  8  6  1
	#    6  6  1  8  6  1
	#    6  6  6  8  6  1
	#    6  6  8  8  6  1
	#    6  8  1  8  6  1
	#    6  8  6  8  6  1
	#    6  8  8  8  6  1
	#    8  1  1  8  6  1
	#    8  1  6  8  6  1
	#    8  1  8  8  6  1
	#    8  6  1  8  6  1
	#    8  6  6  8  6  1
	#    8  6  8  8  6  1
	#    8  8  1  8  6  1
	#    8  8  6  8  6  1
	#    8  8  8  8  6  1
	task.combination("$$$861")
end

main()

Output

 Given String : 1$2$3
 1 1 2 1 3
 1 1 2 2 3
 1 1 2 3 3
 1 2 2 1 3
 1 2 2 2 3
 1 2 2 3 3
 1 3 2 1 3
 1 3 2 2 3
 1 3 2 3 3

 Given String : $$$861
 1 1 1 8 6 1
 1 1 6 8 6 1
 1 1 8 8 6 1
 1 6 1 8 6 1
 1 6 6 8 6 1
 1 6 8 8 6 1
 1 8 1 8 6 1
 1 8 6 8 6 1
 1 8 8 8 6 1
 6 1 1 8 6 1
 6 1 6 8 6 1
 6 1 8 8 6 1
 6 6 1 8 6 1
 6 6 6 8 6 1
 6 6 8 8 6 1
 6 8 1 8 6 1
 6 8 6 8 6 1
 6 8 8 8 6 1
 8 1 1 8 6 1
 8 1 6 8 6 1
 8 1 8 8 6 1
 8 6 1 8 6 1
 8 6 6 8 6 1
 8 6 8 8 6 1
 8 8 1 8 6 1
 8 8 6 8 6 1
 8 8 8 8 6 1
import scala.collection.mutable._;
// Scala Program 
// Print all combinations of other digits on a '$' place of given sequence
class Sequence()
{
	def findCombination(num: String, digit: Array[Boolean], 
      result: String, index: Int, n: Int): Unit = {
		if (index == n)
		{
			println(result);
			return;
		}
		if (num.charAt(index) == '$')
		{
			var i: Int = 0;
			while (i <= 9)
			{
				if (digit(i) == true)
				{
					findCombination(num, digit, 
                                    result + " " + i.toString(), 
                                    index + 1, n);
				}
				i += 1;
			}
		}
		else
		{
			findCombination(num, digit, 
                            result + " " + num.charAt(index).toString(), 
                            index + 1, n);
		}
	}
	def combination(num: String): Unit = {
		var n: Int = num.length();
		if (n < 0)
		{
			return;
		}
		println("\n Given String : " + num);
		var digit: Array[Boolean] = Array.fill[Boolean](10)(false);
		var i: Int = 0;
		// Find other digit
		while (i < n)
		{
			if (num.charAt(i) != '$')
			{
				// Active digit status
				digit(num.charAt(i).toInt - '0'.toInt) = true;
			}
			i += 1;
		}
		findCombination(num, digit, "", 0, n);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Sequence = new Sequence();
		// Text : 1$2$3
		// ------------------
		//   1  1  2  1  3
		//   1  1  2  2  3
		//   1  1  2  3  3
		//   1  2  2  1  3
		//   1  2  2  2  3
		//   1  2  2  3  3
		//   1  3  2  1  3
		//   1  3  2  2  3
		//   1  3  2  3  3
		task.combination("1$2$3");
		// Text : $$$861
		//   1  1  1  8  6  1
		//   1  1  6  8  6  1
		//   1  1  8  8  6  1
		//   1  6  1  8  6  1
		//   1  6  6  8  6  1
		//   1  6  8  8  6  1
		//   1  8  1  8  6  1
		//   1  8  6  8  6  1
		//   1  8  8  8  6  1
		//   6  1  1  8  6  1
		//   6  1  6  8  6  1
		//   6  1  8  8  6  1
		//   6  6  1  8  6  1
		//   6  6  6  8  6  1
		//   6  6  8  8  6  1
		//   6  8  1  8  6  1
		//   6  8  6  8  6  1
		//   6  8  8  8  6  1
		//   8  1  1  8  6  1
		//   8  1  6  8  6  1
		//   8  1  8  8  6  1
		//   8  6  1  8  6  1
		//   8  6  6  8  6  1
		//   8  6  8  8  6  1
		//   8  8  1  8  6  1
		//   8  8  6  8  6  1
		//   8  8  8  8  6  1
		task.combination("$$$861");
	}
}

Output

 Given String : 1$2$3
 1 1 2 1 3
 1 1 2 2 3
 1 1 2 3 3
 1 2 2 1 3
 1 2 2 2 3
 1 2 2 3 3
 1 3 2 1 3
 1 3 2 2 3
 1 3 2 3 3

 Given String : $$$861
 1 1 1 8 6 1
 1 1 6 8 6 1
 1 1 8 8 6 1
 1 6 1 8 6 1
 1 6 6 8 6 1
 1 6 8 8 6 1
 1 8 1 8 6 1
 1 8 6 8 6 1
 1 8 8 8 6 1
 6 1 1 8 6 1
 6 1 6 8 6 1
 6 1 8 8 6 1
 6 6 1 8 6 1
 6 6 6 8 6 1
 6 6 8 8 6 1
 6 8 1 8 6 1
 6 8 6 8 6 1
 6 8 8 8 6 1
 8 1 1 8 6 1
 8 1 6 8 6 1
 8 1 8 8 6 1
 8 6 1 8 6 1
 8 6 6 8 6 1
 8 6 8 8 6 1
 8 8 1 8 6 1
 8 8 6 8 6 1
 8 8 8 8 6 1
import Foundation;
// Swift 4 Program 
// Print all combinations of other digits on a '$' place of given sequence
class Sequence
{
	func findCombination(_ num: [Character], 
                          _ digit: [Bool], 
      _ result: String, 
      _ index: Int, _ n: Int)
	{
		if (index == n)
		{
			print(result);
			return;
		}
		if (num[index] == "$")
		{
			var i: Int = 0;
			while (i <= 9)
			{
				if (digit[i] == true)
				{
					self.findCombination(num, digit, result + " "
						+ String(i), index + 1, n);
				}
				i += 1;
			}
		}
		else
		{
			self.findCombination(num, digit, 
                                 result + " " + String(num[index]), 
                                 index + 1, n);
		}
	}
	func combination(_ num: String)
	{
		let n: Int = num.count;
		if (n < 0)
		{
			return;
		}
		print("\n Given String : ", num);
		var digit: [Bool] = Array(repeating: false, count: 10);
      	let value = Array(num);
		var i: Int = 0;
		// Find other digit
		while (i < n)
		{
			if (value[i]  != "$")
			{
				// Active digit status
				digit[Int(UnicodeScalar(String(value[i]))!.value) -
                      48] = true;
			}
			i += 1;
		}
		self.findCombination(value, digit, "", 0, n);
	}
}
func main()
{
	let task: Sequence = Sequence();
	// Text : 1$2$3
	// ------------------
	//   1  1  2  1  3
	//   1  1  2  2  3
	//   1  1  2  3  3
	//   1  2  2  1  3
	//   1  2  2  2  3
	//   1  2  2  3  3
	//   1  3  2  1  3
	//   1  3  2  2  3
	//   1  3  2  3  3
	task.combination("1$2$3");
	// Text : $$$861
	//   1  1  1  8  6  1
	//   1  1  6  8  6  1
	//   1  1  8  8  6  1
	//   1  6  1  8  6  1
	//   1  6  6  8  6  1
	//   1  6  8  8  6  1
	//   1  8  1  8  6  1
	//   1  8  6  8  6  1
	//   1  8  8  8  6  1
	//   6  1  1  8  6  1
	//   6  1  6  8  6  1
	//   6  1  8  8  6  1
	//   6  6  1  8  6  1
	//   6  6  6  8  6  1
	//   6  6  8  8  6  1
	//   6  8  1  8  6  1
	//   6  8  6  8  6  1
	//   6  8  8  8  6  1
	//   8  1  1  8  6  1
	//   8  1  6  8  6  1
	//   8  1  8  8  6  1
	//   8  6  1  8  6  1
	//   8  6  6  8  6  1
	//   8  6  8  8  6  1
	//   8  8  1  8  6  1
	//   8  8  6  8  6  1
	//   8  8  8  8  6  1
	task.combination("$$$861");
}
main();

Output

 Given String :  1$2$3
 1 1 2 1 3
 1 1 2 2 3
 1 1 2 3 3
 1 2 2 1 3
 1 2 2 2 3
 1 2 2 3 3
 1 3 2 1 3
 1 3 2 2 3
 1 3 2 3 3

 Given String :  $$$861
 1 1 1 8 6 1
 1 1 6 8 6 1
 1 1 8 8 6 1
 1 6 1 8 6 1
 1 6 6 8 6 1
 1 6 8 8 6 1
 1 8 1 8 6 1
 1 8 6 8 6 1
 1 8 8 8 6 1
 6 1 1 8 6 1
 6 1 6 8 6 1
 6 1 8 8 6 1
 6 6 1 8 6 1
 6 6 6 8 6 1
 6 6 8 8 6 1
 6 8 1 8 6 1
 6 8 6 8 6 1
 6 8 8 8 6 1
 8 1 1 8 6 1
 8 1 6 8 6 1
 8 1 8 8 6 1
 8 6 1 8 6 1
 8 6 6 8 6 1
 8 6 8 8 6 1
 8 8 1 8 6 1
 8 8 6 8 6 1
 8 8 8 8 6 1
// Kotlin Program 
// Print all combinations of other digits on a '$' place of given sequence
class Sequence
{
	fun findCombination(num: String, digit: Array < Boolean > , 
                         result: String, index: Int, n: Int): Unit
	{
		if (index == n)
		{
			println(result);
			return;
		}
		if (num.get(index) == '$')
		{
			var i: Int = 0;
			while (i <= 9)
			{
				if (digit[i] == true)
				{
					this.findCombination(num, digit, 
                    result + " " + i.toString(), index + 1, n);
				}
				i += 1;
			}
		}
		else
		{
			this.findCombination(num, digit, 
            result + " " + num.get(index).toString(), index + 1, n);
		}
	}
	fun combination(num: String): Unit
	{
		val n: Int = num.length;
		if (n < 0)
		{
			return;
		}
		println("\n Given String : " + num);
		val digit: Array < Boolean > = Array(10)
		{
			false
		};
		var i: Int = 0;
		// Find other digit
		while (i < n)
		{
			if (num.get(i) != '$')
			{
				// Active digit status
				digit[num.get(i).toInt() - '0'.toInt()] = true;
			}
			i += 1;
		}
		this.findCombination(num, digit, "", 0, n);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Sequence = Sequence();
	// Text : 1$2$3
	// ------------------
	//   1  1  2  1  3
	//   1  1  2  2  3
	//   1  1  2  3  3
	//   1  2  2  1  3
	//   1  2  2  2  3
	//   1  2  2  3  3
	//   1  3  2  1  3
	//   1  3  2  2  3
	//   1  3  2  3  3
	task.combination("1$2$3");
	// Text : $$$861
	//   1  1  1  8  6  1
	//   1  1  6  8  6  1
	//   1  1  8  8  6  1
	//   1  6  1  8  6  1
	//   1  6  6  8  6  1
	//   1  6  8  8  6  1
	//   1  8  1  8  6  1
	//   1  8  6  8  6  1
	//   1  8  8  8  6  1
	//   6  1  1  8  6  1
	//   6  1  6  8  6  1
	//   6  1  8  8  6  1
	//   6  6  1  8  6  1
	//   6  6  6  8  6  1
	//   6  6  8  8  6  1
	//   6  8  1  8  6  1
	//   6  8  6  8  6  1
	//   6  8  8  8  6  1
	//   8  1  1  8  6  1
	//   8  1  6  8  6  1
	//   8  1  8  8  6  1
	//   8  6  1  8  6  1
	//   8  6  6  8  6  1
	//   8  6  8  8  6  1
	//   8  8  1  8  6  1
	//   8  8  6  8  6  1
	//   8  8  8  8  6  1
	task.combination("$$$861");
}

Output

 Given String : 1$2$3
 1 1 2 1 3
 1 1 2 2 3
 1 1 2 3 3
 1 2 2 1 3
 1 2 2 2 3
 1 2 2 3 3
 1 3 2 1 3
 1 3 2 2 3
 1 3 2 3 3

 Given String : $$$861
 1 1 1 8 6 1
 1 1 6 8 6 1
 1 1 8 8 6 1
 1 6 1 8 6 1
 1 6 6 8 6 1
 1 6 8 8 6 1
 1 8 1 8 6 1
 1 8 6 8 6 1
 1 8 8 8 6 1
 6 1 1 8 6 1
 6 1 6 8 6 1
 6 1 8 8 6 1
 6 6 1 8 6 1
 6 6 6 8 6 1
 6 6 8 8 6 1
 6 8 1 8 6 1
 6 8 6 8 6 1
 6 8 8 8 6 1
 8 1 1 8 6 1
 8 1 6 8 6 1
 8 1 8 8 6 1
 8 6 1 8 6 1
 8 6 6 8 6 1
 8 6 8 8 6 1
 8 8 1 8 6 1
 8 8 6 8 6 1
 8 8 8 8 6 1

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