WhiZTiM

Leetcode-17 - Fast

Jul 7th, 2022
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2.     private static final char[][] FAST_MAP = initChar();
  3.     private static char[][] initChar() {
  4.         char[][] result = new char[10][];
  5.         result[0] = new char[]{};
  6.         result[1] = new char[]{};
  7.         result[2] = new char[]{'a', 'b', 'c'};
  8.         result[3] = new char[]{'d', 'e', 'f'};
  9.         result[4] = new char[]{'g', 'h', 'i'};
  10.         result[5] = new char[]{'j', 'k', 'l'};
  11.         result[6] = new char[]{'m', 'n', 'o'};
  12.         result[7] = new char[]{'p', 'q', 'r', 's'};
  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.         }
  24.         result = new ArrayList<>();
  25.         compute(digits, 0, new StringBuilder());
  26.         return result;
  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. }
Add Comment
Please, Sign In to add comment