-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhoneletterCombinations.java
43 lines (38 loc) · 1.2 KB
/
PhoneletterCombinations.java
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
class PhoneletterCombinations {
/*
Runtime: 4 ms, faster than 40.57% of Java online submissions for Letter Combinations of a Phone Number.
Memory Usage: 39 MB, less than 34.86% of Java online submissions for Letter Combinations of a Phone Number.
*/
public List<String> letterCombinations(String digits) {
ArrayList<String> result=new ArrayList<>();
if(digits.length()==0||digits==null)
return result;
String [] mappings={
"0",
"1",
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz"
};
letterCombination(result,digits,"",0,mappings);
return result;
}
public void letterCombination(List<String> result,String digits, String value, int index, String [] mappings)
{
if(index==digits.length())
{
result.add(value);
return;
}
String vals=mappings[digits.charAt(index)-'0'];
for(int i=0;i<vals.length();i++)
{
letterCombination(result,digits,value+vals.charAt(i),index+1,mappings);
}
}
}