List All Possible Words From Phone Digits

package com.thealgorithms.strings;

import java.util.*;

public class List_all_Possible_Words_From_Phone_Digits {

    static Character[][] numberToCharMap;

    private static List<String> printWords(int[] numbers,
            int len,
            int numIndex,
            String s) {
        if (len == numIndex) {
            return new ArrayList<>(Collections.singleton(s));
        }

        List<String> stringList = new ArrayList<>();

        for (int i = 0;
                i < numberToCharMap[numbers[numIndex]].length; i++) {
            String sCopy
                    = String.copyValueOf(s.toCharArray());
            sCopy = sCopy.concat(
                    numberToCharMap[numbers[numIndex]][i].toString());
            stringList.addAll(printWords(numbers, len,
                    numIndex + 1,
                    sCopy));
        }
        return stringList;
    }

    private static void printWords(int[] numbers) {
        generateNumberToCharMap();
        List<String> stringList
                = printWords(numbers, numbers.length, 0, "");
        stringList.stream().forEach(System.out::println);
    }

    private static void generateNumberToCharMap() {
        numberToCharMap = new Character[10][5];
        numberToCharMap[0] = new Character[]{'\0'};
        numberToCharMap[1] = new Character[]{'\0'};
        numberToCharMap[2] = new Character[]{'a', 'b', 'c'};
        numberToCharMap[3] = new Character[]{'d', 'e', 'f'};
        numberToCharMap[4] = new Character[]{'g', 'h', 'i'};
        numberToCharMap[5] = new Character[]{'j', 'k', 'l'};
        numberToCharMap[6] = new Character[]{'m', 'n', 'o'};
        numberToCharMap[7] = new Character[]{'p', 'q', 'r', 's'};
        numberToCharMap[8] = new Character[]{'t', 'u', 'v'};
        numberToCharMap[9] = new Character[]{'w', 'x', 'y', 'z'};
    }

// Driver code 
    public static void main(String[] args) {
        int number[] = {2, 3, 4};
        printWords(number);
    }
}
Algerlogo

Β© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.