Java ArrayList to Print all Possible Words from Phone Digits


You are given a keypad of mobile, and the keys that need to be pressed, your task is to print all the words which are possible to generate by pressing these numbers.

Input Output Scenarios

Let’s go through an example to understand this more:

Input

str = "32" 

Output

[gd, hd, id, ge, je, ie, gf, hf, if]

The characters that can be formed by pressing 3 is g, h, i and by pressing 2 characters d, e, f can be formed. So all the words will be a combination where first character belongs to g, h, i and 2nd character belongs to d, e, f.

To print all possible words from phone digits, you can use a recursive approach that generates all possible combinations of letters corresponding to the phone digits.

Steps/Approach

In this example, we define a phoneMap that maps each digit to a list of corresponding letters. We also define a getWordsFromDigits function that takes the current digits, the current word being constructed, and a list of all possible words.

The getWordsFromDigits function first checks if there are no more digits to process. If so, it adds the current word to the list of all possible words and returns. Otherwise, it retrieves the letters corresponding to the first digit, and recursively calls getWordsFromDigits for each letter, passing in the remaining digits and the updated current word.

In the main function, we initialize the phone digits and an empty list of words and call getWordsFromDigits with these parameters. Finally, we loop through the list of all possible words and print them out one by one.

Example

Here is a sample code:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PhoneDigitsConvertToWords {   
   //hashmap to store phone digits and character
    private static Map<Character, List<Character>> phoneDigitMap = new HashMap<>();
   
    static {
        phoneDigitMap.put('2', Arrays.asList('a', 'b', 'c'));
        phoneDigitMap.put('3', Arrays.asList('d', 'e', 'f'));
        phoneDigitMap.put('4', Arrays.asList('g', 'h', 'i'));
        phoneDigitMap.put('5', Arrays.asList('j', 'k', 'l'));
        phoneDigitMap.put('6', Arrays.asList('m', 'n', 'o'));
        phoneDigitMap.put('7', Arrays.asList('p', 'q', 'r', 's'));
        phoneDigitMap.put('8', Arrays.asList('t', 'u', 'v'));
        phoneDigitMap.put('9', Arrays.asList('w', 'x', 'y', 'z'));
    }
   
   //Driver method
    public static void main(String[] args) {
        String digitsStr = "23";
        List<String> words = new ArrayList<>();
        getWordsFromPhoneDigits(digitsStr, "", words);
        for (String word : words) {
            System.out.println(word);
        }
    }
   
   //method to get words from the input phone digit string
    private static void getWordsFromPhoneDigits(String digitsStr, String currentWord, List<String> words) {
        if (digitsStr.length() == 0) {
            words.add(currentWord);
            return;
        }
        char digit = digitsStr.charAt(0);
        List<Character> letters = phoneDigitMap.get(digit);
        for (Character letter : letters) {
            getWordsFromPhoneDigits(digitsStr.substring(1), currentWord + letter, words);
        }
    }
}

Output

ad
ae
af
bd
be
bf
cd
ce
cf

Time and Space Complexity

The solution has a time complexity of O(3^N * 4^M), where N is the quantity of digits that correspond to 3 letters (2, 3, 4, 5, 6, 8) and M is the quantity of digits that belong to 4 letters. (7, 9). This is because there are three possible letters for each digit that corresponds to three letters, and four possible letters for each digit that corresponds to four letters. There are therefore 3^N * 4^M potential letter combinations.

Because that is the most words that may be generated, the solution's space complexity is also O(3^N * 4^M). In the worst scenario, every letter combination will produce a word that is valid, hence we must store every word in the list of words.

Updated on: 22-Aug-2023

153 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements