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