Advertisement
GokulDeep

Letter Combinations

Feb 25th, 2024
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | Source Code | 0 0
  1. class Solution {
  2.     List<String> outputList = new ArrayList<>();
  3.  
  4.     public List<String> letterCombinations(String digits) {
  5.         Map<Integer, char[]> keypadMap = new HashMap<>();
  6.  
  7.         keypadMap.put(2, new char[] { 'a', 'b', 'c' });
  8.         keypadMap.put(3, new char[] { 'd', 'e', 'f' });
  9.         keypadMap.put(4, new char[] { 'g', 'h', 'i' });
  10.         keypadMap.put(5, new char[] { 'j', 'k', 'l' });
  11.         keypadMap.put(6, new char[] { 'm', 'n', 'o' });
  12.         keypadMap.put(7, new char[] { 'p', 'q', 'r', 's' });
  13.         keypadMap.put(8, new char[] { 't', 'u', 'v' });
  14.         keypadMap.put(9, new char[] { 'w', 'x', 'y', 'z' });
  15.  
  16.         int i = 0;
  17.         char temp[] = new char[digits.length()];
  18.         printAll(temp, i, digits, keypadMap);
  19.  
  20.         return outputList;
  21.     }
  22.  
  23.     public void printAll(char[] temp, int i, String digits, Map<Integer, char[]> keypadMap) {
  24.         char[] charArray = digits.toCharArray();
  25.         if (i > charArray.length - 1 && temp != null) {
  26.             if (temp.length != 0) {
  27.                 outputList.add(new String(temp));
  28.             }
  29.             return;
  30.         }
  31.  
  32.         for (int j = 0; j < keypadMap.get(charArray[i] - '0').length; j++) {
  33.             temp[i] = keypadMap.get(charArray[i] - '0')[j];
  34.  
  35.             printAll(temp, i + 1, digits, keypadMap);
  36.         }
  37.  
  38.     }
  39. }
Tags: Java
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement