1
+ /*
2
+ Author: King, wangjingui@outlook.com
3
+ Date: Dec 20, 2014
4
+ Problem: Letter Combinations of a Phone Number
5
+ Difficulty: Medium
6
+ Source: https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/
7
+ Notes:
8
+ Given a digit string, return all possible letter combinations that the number could represent.
9
+ A mapping of digit to letters (just like on the telephone buttons) is given below.
10
+ Input:Digit string "23"
11
+ Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
12
+ Note:
13
+ Although the above answer is in lexicographical order, your answer could be in any order you want.
14
+
15
+ Solution: ...
16
+ */
17
+
18
+ public class Solution {
19
+ public List <String >letterCombinations (String digits ) {
20
+ ArrayList <String >res =new ArrayList <String >();
21
+ String []keyboard =new String []{" " ,"" ,"abc" ,"def" ,"ghi" ,"jkl" ,"mno" ,"pqrs" ,"tuv" ,"wxyz" };
22
+ letterCombinationsRe (keyboard ,res ,digits ,"" );
23
+ return res ;
24
+ }
25
+ public void letterCombinationsRe (String []keyboard ,ArrayList <String >res ,String digits ,String s ) {
26
+ if (s .length () ==digits .length ()) {
27
+ res .add (s );
28
+ return ;
29
+ }
30
+ String letters =keyboard [digits .charAt (s .length ()) -'0' ];
31
+ for (int i =0 ;i <letters .length (); ++i ) {
32
+ letterCombinationsRe (keyboard ,res ,digits ,s +letters .charAt (i ));
33
+ }
34
+ }
35
+ }