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

Given a number, print all possible combinations of strings that can be used to dial the given number in a phone with following specifications.

In the given phone, we can dial,
2 using A or B or C,
3 using D or E or F,
……………….
8 using T or U or V,
9 using W or X or Y or Z,
1 using only 1
0 using 0.

For example if 23, is the given phone number, the program should print AD, AE, AF, BD, BE, BF, CD, CE, CF

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is to store digit to characters mapping in hash map. The map stores all characters that can be used dial a digit. We place every possible character for current digit and recur for remaining digits. Below is Java implementation of this idea.

```// Java program to print all possible key strings
// that can be used to dial a phone number.
import java.util.HashMap;

class ConvertToString
{
// A Recursive function to print all combinations
// that can be used to dial a given number.
// phNo ==> Given Phone Number
// i    ==> Current digit of phNo to be processed
// hm   ==> Stores characters that can be used to
//          to dial a digit.
// str  ==> Current output string
static void printStrings(String phNo, int i,
HashMap<Character, String> hm,
StringBuilder str)
{
// If all digits are processed, print output
// string
if (i == phNo.length())
{
System.out.print(str + " ");
return;
}

// Get current digit of phNo, and recur for all
// characters that can be used to dial it.
String s = hm.get(phNo.charAt(i));
for (int j = 0; j < s.length(); j++)
{
str.append(s.charAt(j));
printStrings(phNo, i+1, hm, str);
str.deleteCharAt(str.length()-1);
}
}

// Prints all possible combinations of strings that
// can be used to dial c[].
static void printStringForNumber(String phNo)
{
// Create a HashMap
HashMap<Character, String> hm =
new HashMap<Character, String>();

// For every digit, store characters that can
// be used to dial it.
hm.put('2', "ABC");
hm.put('3', "DEF");
hm.put('4', "GHI");
hm.put('5', "JKL");
hm.put('6', "MNO");
hm.put('7', "PQRS");
hm.put('8', "TUV");
hm.put('9', "WXYZ");
hm.put('1', "1");
hm.put('0', "0");

// Create a string to store a particular output
// string
StringBuilder str = new StringBuilder();

// Call recursive function
printStrings(phNo, 0, hm, str);
}

// Driver code to test above methods
public static void main(String args[])
{
// Prints
printStringForNumber("23");
}
}
```

Output :

`AD AE AF BD BE BF CD CE CF `

This article is contributed by Phalgun. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
2.8 Average Difficulty : 2.8/5.0
Based on 17 vote(s)

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.