-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathPrintAllPossibleWordsFromMobileDigits.java
57 lines (45 loc) · 1.79 KB
/
PrintAllPossibleWordsFromMobileDigits.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.geeksforgeeks.array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PrintAllPossibleWordsFromMobileDigits {
private static Map<Character, String> digitStringMap = new HashMap<>();
static {
digitStringMap.put('1', "");
digitStringMap.put('2', "abc");
digitStringMap.put('3', "def");
digitStringMap.put('4', "ghi");
digitStringMap.put('5', "jkl");
digitStringMap.put('6', "mno");
digitStringMap.put('7', "pqrs");
digitStringMap.put('8', "tuv");
digitStringMap.put('9', "wxyz");
digitStringMap.put('0', "");
}
public static void main(String[] args) {
printAllPossibleWord("234");
}
public static void printAllPossibleWord(String digitInMobile) {
List<String> RESULT = new ArrayList<>();
List<String> PRE_RESULT = new ArrayList<>();
RESULT.add(""); // Hypothetical requirement
for (int i = 0; i < digitInMobile.length(); i++) { // Traverse the digit
// Get all letters on this digit
String letters = digitStringMap.get(digitInMobile.charAt(i));
if (letters.length() == 0) { // We have to skip the keys in Mobile containing no letters, Such as 1,0,*,#
continue;
}
// Let's append these letters into the words existing in RESULT
for (String str : RESULT) {
// Let's add these letter 1 by one
for (char letter : letters.toCharArray()) {
PRE_RESULT.add(str + letter);
}
}
RESULT = PRE_RESULT;
PRE_RESULT = new ArrayList<>(); // We need fresh result for next digit
}
System.out.println(RESULT);
}
}