• API
• FAQ
• Tools
• Archive
daily pastebin goal
28%
SHARE
TWEET

# Untitled

a guest Jan 12th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. class Solution {
2.     public static HashMap<Integer, List<String>> map = new HashMap<>();
3.     public static List<String> result = new LinkedList();
4.     public List<String> letterCombinations(String digits) {
5.         Map();
6.         StringBuilder sb = new StringBuilder();
7.         int length = digits.length();
8.         combineLetters(sb, digits, length);
9.         return result;
10.     }
11.
12.     public void Map() {
13.         map.put(0, new ArrayList<String>(Arrays.asList(" ")));
14.         map.put(1, new ArrayList<String>(Arrays.asList("")));
15.         map.put(2, new ArrayList<String>(Arrays.asList("a", "b", "c")));
16.         map.put(3, new ArrayList<String>(Arrays.asList("d", "e", "f")));
17.         map.put(4, new ArrayList<String>(Arrays.asList("g", "h", "i")));
18.         map.put(5, new ArrayList<String>(Arrays.asList("j", "k", "l")));
19.         map.put(6, new ArrayList<String>(Arrays.asList("m", "n", "o")));
20.         map.put(7, new ArrayList<String>(Arrays.asList("p", "q", "r", "s")));
21.         map.put(8, new ArrayList<String>(Arrays.asList("t", "u", "v")));
22.         map.put(9, new ArrayList<String>(Arrays.asList("w", "x", "y", "z")));
23.     }
24.
25.     public void combineLetters(StringBuilder string, String digits, int length) {
26.         if (string.length() == length) {
27.             if (string.length() != 0) {
29.             }
30.             return;
31.         }
32.         int key = 0;
33.         if (digits.length() != 0) {
34.             key = Integer.parseInt(Character.toString(digits.charAt(0)));
35.         } else {
36.             return;
37.         }
38.         for (int i = 0; i < map.get(key).size(); i++) {
39.             string.append(map.get(key).get(i));
40.             combineLetters(string, digits.substring(1), length);
41.             if (string.length() != 0) {
42.                 string.setLength(string.length() - 1);
43.             }
44.         }
45.     }
46. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top