-
Notifications
You must be signed in to change notification settings - Fork 16
/
LetterCombinationsOfAPhoneNumber.java
35 lines (32 loc) · 1.29 KB
/
LetterCombinationsOfAPhoneNumber.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
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class LetterCombinationsOfAPhoneNumber {
private static final Map<Character, Set<Character>> NUMBER_TO_CHARS = Map.ofEntries(
Map.entry('2', Set.of('a', 'b', 'c')),
Map.entry('3', Set.of('d', 'e', 'f')),
Map.entry('4', Set.of('g', 'h', 'i')),
Map.entry('5', Set.of('j', 'k', 'l')),
Map.entry('6', Set.of('m', 'n', 'o')),
Map.entry('7', Set.of('p', 'q', 'r', 's')),
Map.entry('8', Set.of('t', 'u', 'v')),
Map.entry('9', Set.of('w', 'x', 'y', 'z'))
);
public List<String> letterCombinations(String digits) {
if (digits.isEmpty()) return Collections.emptyList();
final List<String> result = new ArrayList<>();
addLetterCombinationsToResult(digits, 0, "", result);
return result;
}
private void addLetterCombinationsToResult(String s, int i, String current, List<String> result) {
if (i == s.length()) {
result.add(current);
return;
}
for (char character : NUMBER_TO_CHARS.get(s.charAt(i))) {
addLetterCombinationsToResult(s, i + 1, current + character, result);
}
}
}