-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path017_LetterCombinationsOfAPhoneNumber17.java
104 lines (92 loc) · 3.44 KB
/
017_LetterCombinationsOfAPhoneNumber17.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
class Solution {
private List<String> bfs(int[] numbers, int n, String[] table) {
if (n == 0) {
return Collections.emptyList();
}
List<String> result = new ArrayList<>();
Queue<String> queue = new ArrayDeque<>();
queue.offer("");
while (!queue.isEmpty()) {
String current = queue.poll();
if (current.length() == n) {
result.add(current);
}
else {
for (char letter : table[numbers[current.length()]].toCharArray()) {
queue.offer(current + letter);
}
}
}
return result;
}
public List<String> letterCombinations(String digits) {
String[] table = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
int n = digits.length();
int[] numbers = new int[n];
for (int i = 0; i < n; i ++) {
char k = digits.charAt(i);
numbers[i] = Character.getNumericValue(k);
}
return bfs(numbers, n, table);
}
}
****************************************************************************************************************
class Solution {
public static final String[] codes = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public List<String> letterCombinations(String digits) {
List<String> result = new ArrayList<>();
if (digits.length() == 0) {
return result;
}
char[] partial = new char[digits.length()];
int start = 0;
permHelper(digits, start, partial, result);
return result;
}
public void permHelper(String input, int idx, char[] partial, List<String> result) {
if (idx == input.length()) {
result.add(new String(partial));
} else {
for (int i = 0; i < codes[input.charAt(idx) - '0'].length(); i++) {
String in = codes[input.charAt(idx) - '0'];
partial[idx] = in.charAt(i);
permHelper(input, idx + 1, partial, result);
}
}
}
}
***********************************************************************************************************
class Solution {
private static char[][] phone;
static {
phone = new char[8][];
phone['2' - '2'] = "abc".toCharArray();
phone['3' - '2'] = "def".toCharArray();
phone['4' - '2'] = "ghi".toCharArray();
phone['5' - '2'] = "jkl".toCharArray();
phone['6' - '2'] = "mno".toCharArray();
phone['7' - '2'] = "pqrs".toCharArray();
phone['8' - '2'] = "tuv".toCharArray();
phone['9' - '2'] = "wxyz".toCharArray();
}
public List<String> letterCombinations(String digits) {
List<String> output = new ArrayList<>();
char[] input = digits.toCharArray();
if (input.length == 0) {
return output;
}
backtrack(input, 0, new char[input.length], output);
return output;
}
private void backtrack(char[] input, int pos, char[] path, List<String> output) {
if (pos == input.length) {
output.add(new String(path));
} else {
char[] phoneDigs = phone[input[pos] - '2'];
for (char dig: phoneDigs) {
path[pos] = dig;
backtrack(input, pos + 1, path, output);
}
}
}
}