forked from chuyi2007/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLetterCombinationOfPhoneNumber.java
More file actions
59 lines (58 loc) · 2.16 KB
/
LetterCombinationOfPhoneNumber.java
File metadata and controls
59 lines (58 loc) · 2.16 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class Solution {
public ArrayList<String> letterCombinations(String digits) {
// Start typing your Java solution below
// DO NOT write main() function
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "");
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");
map.put(0, "");
//return naiveRecursion(digits, 0, map);
ArrayList<String> results = new ArrayList<String>();
naiveBackTracking(digits, "", map, results);
return results;
}
//back tracking, clean code
public void naiveBackTracking(String digits, String result, HashMap<Integer, String> map,
ArrayList<String> results){
if(digits.length() == 0)
results.add(result);
else{
String s = map.get(digits.charAt(0) - 48);
for(int i = 0; i < s.length(); ++i)
naiveBackTracking(digits.substring(1), result + s.substring(i, i+1), map, results);
}
}
//recursion, more complicated
public ArrayList<String> naiveRecursion(String digits, int index, HashMap<Integer, String> map){
ArrayList<String> result = new ArrayList<String>();
String s;
if(index == digits.length() - 1){
s = map.get(digits.charAt(index) - 48);
for(int i = 0; i < s.length(); i++){
result.add(s.substring(i, i+1));
}
return result;
}
else if(index < digits.length() - 1){
result = naiveRecursion(digits, index + 1, map);
s = map.get(digits.charAt(index) - 48);
int size = result.size();
for(int j = 0; j < size; ++j){
String x = result.get(j);
for(int i = 0; i < s.length(); ++i){
x += s.substring(i, i+1);
result.add(x);
}
}
result.subList(0, size).clear();
}
return result;
}
}