Skip to content

Commit 560d583

Browse files
solves letter combinations of phone numbers
1 parent 3700b4e commit 560d583

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
| 14 | [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/) | [![Java](assets/java.png)](src/LongestCommonPrefix.java) [![Python](assets/python.png)](python/longest_common_prefix.py) | |
2626
| 15 | [3Sum](https://leetcode.com/problems/3sum) | [![Java](assets/java.png)](src/ThreeSum.java) | |
2727
| 16 | [3Sum Closest](https://leetcode.com/problems/3sum-closest) | [![Java](assets/java.png)](src/ThreeSumClosest.java) | |
28+
| 17 | [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number) | [![Java](assets/java.png)](src/LetterCombinationsOfAPhoneNumber.java) | |
2829
| 20 | [ValidParentheses](https://leetcode.com/problems/valid-parentheses/) | [![Java](assets/java.png)](src/ValidParentheses.java) [![Python](assets/python.png)](python/valid_parentheses.py) | |
2930
| 21 | [Merge 2 Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [![Java](assets/java.png)](src/Merge2SortedLists.java) [![Python](assets/python.png)](python/merge_2_sorted_lists.py) | |
3031
| 26 | [Remove Duplicates From Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [![Java](assets/java.png)](src/RemoveDuplicatesFromSortedArray.java) [![Python](assets/python.png)](python/remove_duplicates_from_sorted_array.py) | |
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.Set;
6+
7+
public class LetterCombinationsOfAPhoneNumber {
8+
private static final Map<Character, Set<Character>> NUMBER_TO_CHARS = Map.ofEntries(
9+
Map.entry('2', Set.of('a', 'b', 'c')),
10+
Map.entry('3', Set.of('d', 'e', 'f')),
11+
Map.entry('4', Set.of('g', 'h', 'i')),
12+
Map.entry('5', Set.of('j', 'k', 'l')),
13+
Map.entry('6', Set.of('m', 'n', 'o')),
14+
Map.entry('7', Set.of('p', 'q', 'r', 's')),
15+
Map.entry('8', Set.of('t', 'u', 'v')),
16+
Map.entry('9', Set.of('w', 'x', 'y', 'z'))
17+
);
18+
19+
public List<String> letterCombinations(String digits) {
20+
if (digits.isEmpty()) return Collections.emptyList();
21+
final List<String> result = new ArrayList<>();
22+
addLetterCombinationsToResult(digits, 0, "", result);
23+
return result;
24+
}
25+
26+
private void addLetterCombinationsToResult(String s, int i, String current, List<String> result) {
27+
if (i == s.length()) {
28+
result.add(current);
29+
return;
30+
}
31+
for (char character : NUMBER_TO_CHARS.get(s.charAt(i))) {
32+
addLetterCombinationsToResult(s, i + 1, current + character, result);
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)