
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print All Possible Words from Phone Digits Using Java ArrayList
In this article, we will learn to generate all possible words from a string of phone digits using Java. Each digit on a mobile keypad corresponds to a set of letters, and our task is to find every possible combination of letters that can be formed by pressing those digits. For instance, if the input is "23," the output would include combinations like "ad," "ae," "af," and so on. We will implement a recursive approach to achieve this, allowing us to systematically generate and print all possible words corresponding to the given digits.
Problem Statement
Write a program in Java to generate all possible words from a string of phone digits. Below is demostration of the same ?
Input
str = "32"
Output
[gd, hd, id, ge, je, ie, gf, hf, if]
The characters that can be formed by pressing 3 are g, h, i, and by pressing 2 characters d, e, f can be formed. So all the words will be a combination where the 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 to print all possible words from phone digits
The following are the steps to print all possible words from phone digits ?
- We will import ArrayList, Arrays, HashMap, List, and Map from the java.util package.
- A phoneDigitMap is defined to map each digit (2-9) to a corresponding list of letters.
- The function getWordsFromPhoneDigits takes three parameters: The current string of digits, the word being constructed, and a list to store all possible words.
- The function checks if there are no more digits left: If true, it adds the constructed word to the list of words and returns, if false, it retrieves the letters for the first digit and then recursively calls itself for each letter, updating the constructed word and passing in the remaining digits.
- The main function initializes the input string of phone digits and an empty list to store the words, it calls the getWordsFromPhoneDigits function with these parameters.
- Finally, it loops through the list of generated words and prints each one.
Java program to print all possible words from phone digits
Below is the code to print all possible words from phone digits ?
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 complexity: O(3^N * 4^M)
Space complexity: O(3^N * 4^M)
Code explanation
In the above program, the PhoneDigitsConvertToWords class converts a string of phone digits into all possible letter combinations based on a phone keypad mapping. It uses a HashMap to associate each digit (2-9) with its corresponding letters. In the main() method, it initializes a string with the digits "23" and an empty list to store results. The recursive method getWordsFromPhoneDigits build combinations by checking if any digits are left to process. If there are no digits left, it adds the current word to the list; otherwise, it retrieves letters for the first digit and recursively generates combinations for the remaining digits. Finally, it prints all possible combinations.