Posted on by Kalkicode
Code Backtracking

All combinations of strings that can be used to dial a number

Here given code implementation process.

//  C program for
//  All combinations of strings that can be used to dial a number
#include <stdio.h>

#include <string.h>

const char *record[] = {
	"0" , "1" , "ABC" , "DEF" , "GHI" , "JKL" , 
    "MNO" , "PQRS" , "TUV" , "WXYZ"
};
// Display calculated result
void printSequence(char result[], int n)
{
	for (int i = 0; i < n; ++i)
	{
		printf("%c", result[i]);
	}
	printf("\n");
}
void findCombination(char *num, char result[], int index, int n)
{
	if (index == n)
	{
		printSequence(result, index);
		return;
	}
	const char *value = record[num[index] - '0'];
  	// Get the length of digit alphabet
	int length = strlen(value);
	for (int i = 0; i < length; ++i)
	{
		result[index] = value[i];
      	// find other solutions using recursively 
		findCombination(num, result, index + 1, n);
	}
}
void combination(char *num)
{
	int n = strlen(num);
	if (n == 0)
	{
		return;
	}
	printf("\n Given Number : %s\n", num);
	char result[n];
	findCombination(num, result, 0, n);
}
int main(int argc, char
	const *argv[])
{
	// Number : 786
	combination("786");
	return 0;
}

Output

 Given Number : 786
PTM
PTN
PTO
PUM
PUN
PUO
PVM
PVN
PVO
QTM
QTN
QTO
QUM
QUN
QUO
QVM
QVN
QVO
RTM
RTN
RTO
RUM
RUN
RUO
RVM
RVN
RVO
STM
STN
STO
SUM
SUN
SUO
SVM
SVN
SVO
// Java Program 
// Print all combinations of other digits on a '$' place of given sequence
public class Sequence
{
	// Display calculated result
	public void findCombination(String[] record, 
  		String num, String result, int index, int n)
	{
		if (index == n)
		{
			System.out.println(result);
			return;
		}
		String value = record[num.charAt(index) - '0'];
		// Get the length of digit alphabet
		int length = value.length();
		for (int i = 0; i < length; ++i)
		{
			// find other solutions using recursively 
			findCombination(record, num, 
                            result + value.charAt(i), index + 1, n);
		}
	}
	public void combination(String num)
	{
		int n = num.length();
		if (n == 0)
		{
			return;
		}
		System.out.println("\n Given Number : " + num);
		String[] record = {
			"0" , "1" , "ABC" , "DEF" , "GHI" , "JKL" , 
           "MNO" , "PQRS" , "TUV" , "WXYZ"
		};
		findCombination(record, num, "", 0, n);
	}
	public static void main(String[] args)
	{
		Sequence task = new Sequence();
		// Number : 786
		task.combination("786");
	}
}

Output

 Given Number : 786
PTM
PTN
PTO
PUM
PUN
PUO
PVM
PVN
PVO
QTM
QTN
QTO
QUM
QUN
QUO
QVM
QVN
QVO
RTM
RTN
RTO
RUM
RUN
RUO
RVM
RVN
RVO
STM
STN
STO
SUM
SUN
SUO
SVM
SVN
SVO
// 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:
		// Display calculated result
		void findCombination(string record[], 
          string num, string result, int index, int n)
		{
			if (index == n)
			{
				cout << result << endl;
				return;
			}
			string value = record[num[index] - '0'];
			// Get the length of digit alphabet
			int length = value.length();
			for (int i = 0; i < length; ++i)
			{
				// find other solutions using recursively 
				this->findCombination(record, num, 
                                      result  +  (value[i]), 
                                      index + 1, n);
			}
		}
	void combination(string num)
	{
		int n = num.length();
		if (n == 0)
		{
			return;
		}
		cout << "\n Given Number : " << num << endl;
		string record[] = {
			"0" , "1" , "ABC" , "DEF" , "GHI" , "JKL" , 
            "MNO" , "PQRS" , "TUV" , "WXYZ"
		};
		this->findCombination(record, num, "", 0, n);
	}
};
int main()
{
	Sequence *task = new Sequence();
	// Number : 786
	task->combination("786");
	return 0;
}

Output

 Given Number : 786
PTM
PTN
PTO
PUM
PUN
PUO
PVM
PVN
PVO
QTM
QTN
QTO
QUM
QUN
QUO
QVM
QVN
QVO
RTM
RTN
RTO
RUM
RUN
RUO
RVM
RVN
RVO
STM
STN
STO
SUM
SUN
SUO
SVM
SVN
SVO
// Include namespace system
using System;
// Csharp Program 
// Print all combinations of other digits on a '$' place of given sequence
public class Sequence
{
	// Display calculated result
	public void findCombination(String[] record, 
  	String num, String result, 
  	int index, int n)
	{
		if (index == n)
		{
			Console.WriteLine(result);
			return;
		}
		String value = record[num[index] - '0'];
		// Get the length of digit alphabet
		int length = value.Length;
		for (int i = 0; i < length; ++i)
		{
			// find other solutions using recursively 
			this.findCombination(record, num, result + value[i], index + 1, n);
		}
	}
	public void combination(String num)
	{
		int n = num.Length;
		if (n == 0)
		{
			return;
		}
		Console.WriteLine("\n Given Number : " + num);
		String[] record = {
			"0" , "1" , "ABC" , "DEF" , "GHI" , "JKL" , "MNO" , "PQRS" , "TUV" , "WXYZ"
		};
		this.findCombination(record, num, "", 0, n);
	}
	public static void Main(String[] args)
	{
		Sequence task = new Sequence();
		// Number : 786
		task.combination("786");
	}
}

Output

 Given Number : 786
PTM
PTN
PTO
PUM
PUN
PUO
PVM
PVN
PVO
QTM
QTN
QTO
QUM
QUN
QUO
QVM
QVN
QVO
RTM
RTN
RTO
RUM
RUN
RUO
RVM
RVN
RVO
STM
STN
STO
SUM
SUN
SUO
SVM
SVN
SVO
package main

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
}
// Display calculated result
func(this Sequence) findCombination(record[] string, 
	num string, result string, index int, n int) {
	if index == n {
		fmt.Println(result)
		return
	}
	var value string = record[num[index] - '0']
	// Get the length of digit alphabet
	var length int = len(value)
	for i := 0 ; i < length ; i++ {
		// find other solutions using recursively 
		this.findCombination(record, num, 
			result + string(value[i]), index + 1, n)
	}
}
func(this Sequence) combination(num string) {
	var n int = len(num)
	if n == 0 {
		return
	}
	fmt.Println("\n Given Number : ", num)
	var record = [] string {
		"0",
		"1",
		"ABC",
		"DEF",
		"GHI",
		"JKL",
		"MNO",
		"PQRS",
		"TUV",
		"WXYZ",
	}
	this.findCombination(record, num, "", 0, n)
}
func main() {
	var task * Sequence = getSequence()
	// Number : 786
	task.combination("786")
}

Output

 Given Number : 786
PTM
PTN
PTO
PUM
PUN
PUO
PVM
PVN
PVO
QTM
QTN
QTO
QUM
QUN
QUO
QVM
QVN
QVO
RTM
RTN
RTO
RUM
RUN
RUO
RVM
RVN
RVO
STM
STN
STO
SUM
SUN
SUO
SVM
SVN
SVO
<?php
// Php Program 
// Print all combinations of other digits on a '$' place of given sequence
class Sequence
{
	// Display calculated result
	public	function findCombination($record, $num, $result, $index, $n)
	{
		if ($index == $n)
		{
			echo($result."\n");
			return;
		}
		$value = $record[ord($num[$index]) - ord('0')];
		// Get the length of digit alphabet
		$length = strlen($value);
		for ($i = 0; $i < $length; ++$i)
		{
			// find other solutions using recursively 
			$this->findCombination($record, $num, 
                                   $result.strval($value[$i]), $index + 1, $n);
		}
	}
	public	function combination($num)
	{
		$n = strlen($num);
		if ($n == 0)
		{
			return;
		}
		echo("\n Given Number : ".$num."\n");
		$record = array("0", "1", "ABC", "DEF", 
                        "GHI", "JKL", "MNO", 
                        "PQRS", "TUV", "WXYZ");
		$this->findCombination($record, $num, "", 0, $n);
	}
}

function main()
{
	$task = new Sequence();
	// Number : 786
	$task->combination("786");
}
main();

Output

 Given Number : 786
PTM
PTN
PTO
PUM
PUN
PUO
PVM
PVN
PVO
QTM
QTN
QTO
QUM
QUN
QUO
QVM
QVN
QVO
RTM
RTN
RTO
RUM
RUN
RUO
RVM
RVN
RVO
STM
STN
STO
SUM
SUN
SUO
SVM
SVN
SVO
// Node JS Program 
// Print all combinations of other digits on a '$' place of given sequence
class Sequence
{
	// Display calculated result
	findCombination(record, num, result, index, n)
	{
		if (index == n)
		{
			console.log(result);
			return;
		}
		var value = record[num.charCodeAt(index)- 48];
		// Get the length of digit alphabet
		var length = value.length;
		for (var i = 0; i < length; ++i)
		{
			// find other solutions using recursively 
			this.findCombination(record, num, 
                                 result + value.charAt(i), index + 1, n);
		}
	}
	combination(num)
	{
		var n = num.length;
		if (n == 0)
		{
			return;
		}
		console.log("\n Given Number : " + num);
		var record = ["0", "1", "ABC", "DEF", "GHI", 
                      "JKL", "MNO", "PQRS", "TUV", "WXYZ"];
		this.findCombination(record, num, "", 0, n);
	}
}

function main()
{
	var task = new Sequence();
	// Number : 786
	task.combination("786");
}
main();

Output

 Given Number : 786
PTM
PTN
PTO
PUM
PUN
PUO
PVM
PVN
PVO
QTM
QTN
QTO
QUM
QUN
QUO
QVM
QVN
QVO
RTM
RTN
RTO
RUM
RUN
RUO
RVM
RVN
RVO
STM
STN
STO
SUM
SUN
SUO
SVM
SVN
SVO
#  Python 3 Program 
#  Print all combinations of other digits on a '$' place of given sequence
class Sequence :
	#  Display calculated result
	def findCombination(self, record, num, result, index, n) :
		if (index == n) :
			print(result)
			return
		
		value = record[ord(num[index]) - ord('0')]
		#  Get the length of digit alphabet
		length = len(value)
		i = 0
		while (i < length) :
			#  find other solutions using recursively 
			self.findCombination(record, num, 
                                 result + str(value[i]), index + 1, n)
			i += 1
		
	
	def combination(self, num) :
		n = len(num)
		if (n == 0) :
			return
		
		print("\n Given Number : ", num)
		record = ["0", "1", "ABC", "DEF", "GHI", 
                  "JKL", "MNO", "PQRS", "TUV", "WXYZ"]
		self.findCombination(record, num, "", 0, n)
	

def main() :
	task = Sequence()
	#  Number : 786
	task.combination("786")

if __name__ == "__main__": main()

Output

 Given Number :  786
PTM
PTN
PTO
PUM
PUN
PUO
PVM
PVN
PVO
QTM
QTN
QTO
QUM
QUN
QUO
QVM
QVN
QVO
RTM
RTN
RTO
RUM
RUN
RUO
RVM
RVN
RVO
STM
STN
STO
SUM
SUN
SUO
SVM
SVN
SVO
#  Ruby Program 
#  Print all combinations of other digits on a '$' place of given sequence
class Sequence 
	#  Display calculated result
	def findCombination(record, num, result, index, n) 
		if (index == n) 
			print(result, "\n")
			return
		end

		value = record[num[index].ord - '0'.ord]
		#  Get the length of digit alphabet
		length = value.length
		i = 0
		while (i < length) 
			#  find other solutions using recursively 
			self.findCombination(record, num,
                                 result + value[i].to_s, index + 1, n)
			i += 1
		end

	end

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

		print("\n Given Number : ", num, "\n")
		record = ["0", "1", "ABC", "DEF", 
                  "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ"]
		self.findCombination(record, num, "", 0, n)
	end

end

def main() 
	task = Sequence.new()
	#  Number : 786
	task.combination("786")
end

main()

Output

 Given Number : 786
PTM
PTN
PTO
PUM
PUN
PUO
PVM
PVN
PVO
QTM
QTN
QTO
QUM
QUN
QUO
QVM
QVN
QVO
RTM
RTN
RTO
RUM
RUN
RUO
RVM
RVN
RVO
STM
STN
STO
SUM
SUN
SUO
SVM
SVN
SVO
import scala.collection.mutable._;
// Scala Program 
// Print all combinations of other digits on a '$' place of given sequence
class Sequence()
{
	// Display calculated result
	def findCombination(record: Array[String], 
      num: String, result: String, 
        index: Int, n: Int): Unit = {
		if (index == n)
		{
			println(result);
			return;
		}
		var value: String = record(num.charAt(index).toInt - 48);
		// Get the length of digit alphabet
		var length: Int = value.length();
		var i: Int = 0;
		while (i < length)
		{
			// find other solutions using recursively 
			findCombination(record, num, 
                            result + value.charAt(i).toString(), 
                            index + 1, n);
			i += 1;
		}
	}
	def combination(num: String): Unit = {
		var n: Int = num.length();
		if (n == 0)
		{
			return;
		}
		println("\n Given Number : " + num);
		var record: Array[String] = Array("0", "1", "ABC", 
                                          "DEF", "GHI", "JKL", 
                                          "MNO", "PQRS", "TUV", "WXYZ");
		findCombination(record, num, "", 0, n);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Sequence = new Sequence();
		// Number : 786
		task.combination("786");
	}
}

Output

 Given Number : 786
PTM
PTN
PTO
PUM
PUN
PUO
PVM
PVN
PVO
QTM
QTN
QTO
QUM
QUN
QUO
QVM
QVN
QVO
RTM
RTN
RTO
RUM
RUN
RUO
RVM
RVN
RVO
STM
STN
STO
SUM
SUN
SUO
SVM
SVN
SVO
import Foundation;
// Swift 4 Program 
// Print all combinations of other digits on a '$' place of given sequence
class Sequence
{
	// Display calculated result
	func findCombination(_ record: [String], 
    _ num: [Character], _ result: String, 
    _ index: Int, _ n: Int)
	{
		if (index == n)
		{
			print(result);
			return;
		}
		let value = 
          Array(record[Int(UnicodeScalar(String(num[index]))!.value) - 48]);
		// Get the length of digit alphabet
		let length: Int = value.count;
		var i: Int = 0;
		while (i < length)
		{
			// find other solutions using recursively 
			self.findCombination(record, num, 
                                 result + String(value[i]), index + 1, n);
			i += 1;
		}
	}
	func combination(_ num: String)
	{
		let n: Int = num.count;
		if (n == 0)
		{
			return;
		}
		print("\n Given Number : ", num);
		let record: [String] = ["0", "1", "ABC", "DEF", 
                                "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ"];
		self.findCombination(record, Array(num), "", 0, n);
	}
}
func main()
{
	let task: Sequence = Sequence();
	// Number : 786
	task.combination("786");
}
main();

Output

 Given Number :  786
PTM
PTN
PTO
PUM
PUN
PUO
PVM
PVN
PVO
QTM
QTN
QTO
QUM
QUN
QUO
QVM
QVN
QVO
RTM
RTN
RTO
RUM
RUN
RUO
RVM
RVN
RVO
STM
STN
STO
SUM
SUN
SUO
SVM
SVN
SVO
// Kotlin Program 
// Print all combinations of other digits on a '$' place of given sequence
class Sequence
{
	// Display calculated result
	fun findCombination(record: Array < String > , 
                         num: String, result: String, 
                         index: Int, n: Int): Unit
	{
		if (index == n)
		{
			println(result);
			return;
		}
		val value: String = record[num.get(index).toInt() - '0'.toInt()];
		// Get the length of digit alphabet
		val length: Int = value.length;
		var i: Int = 0;
		while (i < length)
		{
			// find other solutions using recursively 
			this.findCombination(record, num, 
                                 result + value.get(i).toString(), 
                                 index + 1, n);
			i += 1;
		}
	}
	fun combination(num: String): Unit
	{
		val n: Int = num.length;
		if (n == 0)
		{
			return;
		}
		println("\n Given Number : " + num);
		val record: Array < String > = 
          arrayOf("0", "1", "ABC", "DEF", "GHI", 
                  "JKL", "MNO", "PQRS", "TUV", "WXYZ");
		this.findCombination(record, num, "", 0, n);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Sequence = Sequence();
	// Number : 786
	task.combination("786");
}

Output

 Given Number : 786
PTM
PTN
PTO
PUM
PUN
PUO
PVM
PVN
PVO
QTM
QTN
QTO
QUM
QUN
QUO
QVM
QVN
QVO
RTM
RTN
RTO
RUM
RUN
RUO
RVM
RVN
RVO
STM
STN
STO
SUM
SUN
SUO
SVM
SVN
SVO

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