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

06 Nov, 2020
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 10 using 0.
For example if 23, is the given phone number, the program should print AD, AE, AF, BD, BE, BF, CD, CE, CF
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 
YouTubeGeeksforGeeks507K subscribersAll combinations of strings that can be used to dial a number | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:45•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=tE9EF-2Jw8U" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>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
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Top 50 String Coding Problems for Interviews
Print all the duplicates in the input string
Vigenère Cipher
sprintf() in C
Convert character array to string in C++
String class in Java | Set 1
How to Append a Character to a String in C
Program to count occurrence of a given character in a string
Naive algorithm for Pattern Searching
Boyer Moore Algorithm for Pattern Searching