-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLeetCode-17-Letter-Combinations-of-a-Phone-Number.java
133 lines (107 loc) · 4.28 KB
/
LeetCode-17-Letter-Combinations-of-a-Phone-Number.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
Integer.parseInt(String)
Character.getNumericValue(char)
1.DFS
2.BFS
https://leetcode.com/discuss/24431/my-java-solution-with-fifo-queue
*/
public class Solution {
// 1.DFS (easy but slow)
// public List<String> letterCombinations(String digits) {
// List<String> result = new ArrayList<String>();
// if(digits == null || digits.length() == 0) return result;
// HashMap<Integer, String> map = new HashMap<Integer, String>();
// 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, "");
// List<Character> list = new ArrayList<Character>();
// DFS(digits, map, result, list);
// return result;
// }
// private void DFS(String digits, HashMap<Integer, String> map, List<String> result, List<Character> list){
// //end condition
// if(digits.length() == 0){
// StringBuilder sb = new StringBuilder();
// for(Character ch : list){
// sb.append(ch);
// }
// result.add(sb.toString());
// return;
// }
// String str = map.get(Character.getNumericValue(digits.charAt(0)));
// for(int i = 0; i < str.length(); i++){
// char ch = str.charAt(i);
// list.add(ch);
// DFS(digits.substring(1), map, result, list);
// list.remove(list.size() - 1);
// }
// }
// 2. BFS
// public List<String> letterCombinations(String digits) {
// if (digits == null || digits.length() == 0) return new ArrayList<>();
// HashMap<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> result = new ArrayList<>();
// result.add("");
// for(char c : digits.toCharArray()) {
// List<String> temp = new ArrayList<>();
// String str = map.get(c);
// for (char c2 : str.toCharArray()) {
// for (String s : result) {
// temp.add(s + c2);
// }
// }
// result = temp;
// }
// return result;
// }
// 3.add string to result circle by circle
public List<String> letterCombinations(String digits) {
List<String> result = new ArrayList<String>();
if(digits == null || digits.length() == 0) return result;
result.add(""); //must initialize, as we add string to result circle by circle
String[] map = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
for(int i = 0; i < digits.length(); i++){
String str = map[Character.getNumericValue(digits.charAt(i))];
while(result.get(0).length() == i){
//this means string in result has not reached to the length as digits, can continue add
String curr = result.remove(0);
for(char ch : str.toCharArray()){
result.add(curr + ch);
}
}
}
return result;
}
public List<String> letterCombinations(String digits) {
List<String> result = new ArrayList<>();
if (digits == null || digits.length() == 0) return result;
result.add("");
String[] map = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
for (int i = 0; i < digits.length(); i++) {
String chars = map[digits.charAt(i) - '0'];
List<String> temp = new ArrayList<>();
for (char ch : chars.toCharArray()) {
for (String str : result) {
temp.add(str + ch);
}
}
result.clear();
result.addAll(temp);
}
return result;
}
}