Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- private static final Map<Character, List<Character>> MAPPING = Map.ofEntries(
- Map.entry('1', List.of()),
- Map.entry('2', List.of('a', 'b', 'c')),
- Map.entry('3', List.of('d', 'e', 'f')),
- Map.entry('4', List.of('g', 'h', 'i')),
- Map.entry('5', List.of('j', 'k', 'l')),
- Map.entry('6', List.of('m', 'n', 'o')),
- Map.entry('7', List.of('p', 'q', 'r', 's')),
- Map.entry('8', List.of('t', 'u', 'v')),
- Map.entry('9', List.of('w', 'x', 'y', 'z'))
- );
- private List<String> result = null;
- private void compute(String digits, int index, StringBuilder stringBuilder) {
- if (index == digits.length()) {
- result.add(stringBuilder.toString());
- return;
- }
- MAPPING.get(digits.charAt(index)).forEach(value -> {
- stringBuilder.append(value);
- compute(digits, index + 1, stringBuilder);
- stringBuilder.setLength(stringBuilder.length() - 1);
- });
- }
- public List<String> letterCombinations(String digits) {
- if (digits.isEmpty()) {
- return List.of();
- }
- result = new ArrayList<>();
- compute(digits, 0, new StringBuilder());
- return result;
- }
- }
Add Comment
Please, Sign In to add comment