-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharInPhoneNumber.java
More file actions
40 lines (36 loc) · 1.22 KB
/
charInPhoneNumber.java
File metadata and controls
40 lines (36 loc) · 1.22 KB
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
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class charInPhoneNumber {
public static void main(String[] args){
List<String> list = letterCombinations("23");
System.out.println(list);
}
public static List<String> letterCombinations(String digits) {
if(digits.length() == 0 || digits == null) return new ArrayList<>();
Map<Character, String> map = new HashMap<>();
map.put('2', "abc");
map.put('3', "def");
map.put('4', "ghi");
map.put('5', "jkl");
map.put('6', "mno");
map.put('7', "pqrs");
map.put('8', "tuv");
map.put('9', "wxyz");
List<String> list = new ArrayList<>();
util("", digits, 0, list, map);
return list;
}
public static void util(String s,String digits,int i, List<String> list, Map<Character, String> map ) {
if(i == digits.length()) {
list.add(s);
return;
}
String temp = map.get(digits.charAt(i));
for(int j = 0; j < temp.length() ; j++) {
util(s + temp.charAt(j), digits, i+1, list, map);
}
}
}