-
Notifications
You must be signed in to change notification settings - Fork 1
/
LetterCombinationsPhoneNumber.java
52 lines (42 loc) · 1.28 KB
/
LetterCombinationsPhoneNumber.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
package com.leetcode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
Example 1:
Input: digits = "23"
Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
**/
public class LetterCombinationsPhoneNumber {
class Solution {
HashMap<Character, String> map = new HashMap<>();
public void init(){
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");
}
public List<String> letterCombinations(String digits) {
init();
List<String> result = new ArrayList<>();
if(digits.length() == 0) return result;
dfs(0,"",digits.toCharArray(),result);
return result;
}
public void dfs(int index, String s,char[] arr, List<String> result){
if(index == arr.length){
result.add(s);
return;
}
char ch = arr[index];
String nextList = map.getOrDefault(ch,"");
for(char next : nextList.toCharArray()){
dfs(index+1,s+next,arr,result);
}
}
}
}