-
-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathLetterCombinations.java
More file actions
35 lines (24 loc) · 791 Bytes
/
LetterCombinations.java
File metadata and controls
35 lines (24 loc) · 791 Bytes
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
package leetcode;
import java.util.ArrayList;
import java.util.List;
/**
* Created by nikoo28 on 7/18/19 3:21 AM
*/
class LetterCombinations {
public static List<String> letterCombinations(String digits) {
String[] digitletter = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
List<String> result = new ArrayList<>();
if (digits.length()==0) return result;
result.add("");
for (int i=0; i<digits.length(); i++)
result = combine(digitletter[digits.charAt(i)-'0'],result);
return result;
}
private static List<String> combine(String digit, List<String> l) {
List<String> result = new ArrayList<>();
for (int i=0; i<digit.length(); i++)
for (String x : l)
result.add(x+digit.charAt(i));
return result;
}
}