leetcode solution get(key) keySet() values() Solution

https://leetcode.com/problems/letter-combinations-of-a-phone-number/

Given a digit string, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below.

img

Input:Digit string “23”
Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].

Note:

Although the above answer is in lexicographical order, your answer could be in any order you want.

This is problem is easy except the use of HashMap.

Java has three kinds of Hashtalbe implementations. They are HashTable, HashMap, HashSet. HashTable is a thread safe implementation, but it is not efficient. For a single thread problem, we always use HashMap.

HashMap have several important methods. They are put(key, value), get(key), keySet(), values().

put(key value) will insert the pair key-value into hashtable. For example:

Map<String,String> map = new HashMap<String,String>();
map.put("one","xiaoming");      // it doesn't have add method since 
map.put("two","xiaozhang");     // it does not implement Collection 
map.put("three","xiaoli");      // interface
map.put("four","xiaoliu");

one, two, three, four are keys and xiaoming, xiaozhang, xiaoli, xiaoliu are values.

get(key)

get(key) will return value which combines with key. For example:

System.out.println(map.get("one"));
System.out.println(map.get("two"));
System.out.println(map.get("three"));
System.out.println(map.get("four"));  

The output will be:

xiaoming
xiaozhang
xiaoli
xiaoliu

keySet()

keySet() will return all keys stored in hashtable.

System.out.println(map.keySet());

The output is:

[four, one, two, three]

values()

values() will return all values stored in hashtable.

System.out.println(map.value());

The output is:

[xiaolu, xiaoming, xiaozhang, xiaoli]

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48


* 本代码由九章算法编辑提供。没有版权欢迎转发。
* - 九章算法致力于帮助更多中国人找到好的工作,教师团队均来自硅谷和国内的一线大公司在职工程师。
* - 现有的面试培训课程包括:九章算法班,系统设计班,BAT国内班
* - 更多详情请见官方网站:http://www.jiuzhang.com/
*/


public class {
public ArrayList<String> letterCombinations(String digits) {
ArrayList<String> result = new ArrayList<String>();

if (digits == null || digits.equals("")) {
return result;
}

Map<Character, char[]> map = new HashMap<Character, char[]>();
map.put('0', new char[] {});
map.put('1', new char[] {});
map.put('2', new char[] { 'a', 'b', 'c' });
map.put('3', new char[] { 'd', 'e', 'f' });
map.put('4', new char[] { 'g', 'h', 'i' });
map.put('5', new char[] { 'j', 'k', 'l' });
map.put('6', new char[] { 'm', 'n', 'o' });
map.put('7', new char[] { 'p', 'q', 'r', 's' });
map.put('8', new char[] { 't', 'u', 'v'});
map.put('9', new char[] { 'w', 'x', 'y', 'z' });

StringBuilder sb = new StringBuilder();
helper(map, digits, sb, result);

return result;
}

private void helper(Map<Character, char[]> map, String digits,
StringBuilder sb, ArrayList<String> result)
{

if (sb.length() == digits.length()) {
result.add(sb.toString());
return;
}

for (char c : map.get(digits.charAt(sb.length()))) {
sb.append(c);
helper(map, digits, sb, result);
sb.deleteCharAt(sb.length() - 1);
}
}
}