View difference between Paste ID: PjPBZY3H and 33Ybamyp
SHOW: | | - or go back to the newest paste.
1
class Solution {
2
    private static final char[][] FAST_MAP = initChar();
3-
    private static final Map<Character, List<Character>> MAPPING = Map.ofEntries(
3+
    private static char[][] initChar() {
4-
        Map.entry('1', List.of()),
4+
        char[][] result = new char[10][];
5-
        Map.entry('2', List.of('a', 'b', 'c')),
5+
        result[0] = new char[]{};
6-
        Map.entry('3', List.of('d', 'e', 'f')),
6+
        result[1] = new char[]{};
7-
        Map.entry('4', List.of('g', 'h', 'i')),
7+
        result[2] = new char[]{'a', 'b', 'c'};
8-
        Map.entry('5', List.of('j', 'k', 'l')),
8+
        result[3] = new char[]{'d', 'e', 'f'};
9-
        Map.entry('6', List.of('m', 'n', 'o')),
9+
        result[4] = new char[]{'g', 'h', 'i'};
10-
        Map.entry('7', List.of('p', 'q', 'r', 's')),
10+
        result[5] = new char[]{'j', 'k', 'l'};
11-
        Map.entry('8', List.of('t', 'u', 'v')),
11+
        result[6] = new char[]{'m', 'n', 'o'};
12-
        Map.entry('9', List.of('w', 'x', 'y', 'z'))
12+
        result[7] = new char[]{'p', 'q', 'r', 's'};
13-
    );
13+
        result[8] = new char[]{'t', 'u', 'v'};
14
        result[9] = new char[]{'w', 'x', 'y', 'z'};
15
        return result;
16
    }
17
18
19
    private List<String> result = null;
20
    public List<String> letterCombinations(String digits) {
21
        if (digits.isEmpty()) {
22
            return List.of();
23-
        MAPPING.get(digits.charAt(index)).forEach(value -> {
23+
24
        result = new ArrayList<>();
25
        compute(digits, 0, new StringBuilder());
26
        return result;
27-
        });
27+
28
29
    private void compute(String digits, int index, StringBuilder stringBuilder) {
30
        if (index == digits.length()) {
31
            result.add(stringBuilder.toString());
32
            return;
33
        }
34
35
        for (char value : FAST_MAP[digits.charAt(index) - '0']) {
36
            stringBuilder.append(value);
37
            compute(digits, index + 1, stringBuilder);
38
            stringBuilder.setLength(stringBuilder.length() - 1);
39
        };
40
    }
41
    
42
}