Skip to content

Commit 5b48382

Browse files
author
zhangbo54
committed
17. Letter Combinations of a Phone Number
1 parent c8715ff commit 5b48382

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

src/leetcode/_17_/Main.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package leetcode._17_;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Created by zhangbo54 on 2019-03-04.
7+
*/
8+
public class Main {
9+
public static void main(String[] args) {
10+
Solution solution = new Solution();
11+
System.out.println(solution.letterCombinations("23"));
12+
13+
// Integer.parseInt("1" + Integer.MAX_VALUE);
14+
// -4 -1, -1, 0, 1, 2,
15+
}
16+
}
17+

src/leetcode/_17_/Solution.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package leetcode._17_;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
/**
9+
* Created by zhangbo54 on 2019-03-05.
10+
*/
11+
class Solution {
12+
public List<String> letterCombinations(String digits) {
13+
if (digits.length() == 0) {
14+
return Collections.emptyList();
15+
}
16+
if (digits.length() == 1) {
17+
return singleDigitToStringList(digits.charAt(0));
18+
}
19+
List<String> leftArr = this.letterCombinations(digits.substring(0, digits.length() / 2));
20+
List<String> rightArr = this.letterCombinations(digits.substring(digits.length() / 2));
21+
List<String> result = new ArrayList<>();
22+
for (String left : leftArr) {
23+
for (String right : rightArr) {
24+
result.add(left + right);
25+
}
26+
}
27+
return result;
28+
}
29+
30+
private List<String> singleDigitToStringList(char digit) {
31+
switch (digit) {
32+
case '2':
33+
return Arrays.asList("a", "b", "c");
34+
case '3':
35+
return Arrays.asList("d", "e", "f");
36+
case '4':
37+
return Arrays.asList("g", "h", "i");
38+
case '5':
39+
return Arrays.asList("j", "k", "l");
40+
case '6':
41+
return Arrays.asList("m", "n", "o");
42+
case '7':
43+
return Arrays.asList("p", "q", "r", "s");
44+
case '8':
45+
return Arrays.asList("t", "u", "v");
46+
case '9':
47+
return Arrays.asList("w", "x", "y", "z");
48+
default:
49+
return Arrays.asList("");
50+
}
51+
}
52+
}

src/leetcode/_17_/solution.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
### [17\. Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given a string containing digits from `2-9` inclusive, return all possible letter combinations that the number could represent.
7+
8+
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
9+
10+
![](http://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Telephone-keypad2.svg/200px-Telephone-keypad2.svg.png)
11+
12+
**Example:**
13+
14+
```
15+
Input: "23"
16+
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
17+
```
18+
19+
**Note:**
20+
21+
Although the above answer is in lexicographical order, your answer could be in any order you want.
22+
23+
24+
#### Solution
25+
26+
Language: **Java**
27+
28+
```java
29+
class Solution {
30+
   public List<String> letterCombinations(String digits) {
31+
       if (digits.length() == 0) {
32+
           return Collections.emptyList();
33+
      }
34+
       if (digits.length() == 1) {
35+
           return singleDigitToStringList(digits.charAt(0));
36+
      }
37+
       List<String> leftArr = this.letterCombinations(digits.substring(0, digits.length() / 2));
38+
       List<String> rightArr = this.letterCombinations(digits.substring(digits.length() / 2));
39+
       List<String> result = new ArrayList<>();
40+
       for (String left : leftArr) {
41+
           for (String right : rightArr) {
42+
               result.add(left + right);
43+
          }
44+
      }
45+
       return result;
46+
  }
47+
48+
   private List<String> singleDigitToStringList(char digit) {
49+
       switch (digit) {
50+
           case '2':
51+
               return Arrays.asList("a", "b", "c");
52+
           case '3':
53+
               return Arrays.asList("d", "e", "f");
54+
           case '4':
55+
               return Arrays.asList("g", "h", "i");
56+
           case '5':
57+
               return Arrays.asList("j", "k", "l");
58+
           case '6':
59+
               return Arrays.asList("m", "n", "o");
60+
           case '7':
61+
               return Arrays.asList("p", "q", "r", "s");
62+
           case '8':
63+
               return Arrays.asList("t", "u", "v");
64+
           case '9':
65+
               return Arrays.asList("w", "x", "y", "z");
66+
           default:
67+
               return Arrays.asList("");
68+
      }
69+
  }
70+
}
71+
72+
```

0 commit comments

Comments
 (0)