-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphone.java
More file actions
33 lines (33 loc) · 1.14 KB
/
phone.java
File metadata and controls
33 lines (33 loc) · 1.14 KB
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
public class Solution {
ArrayList<String> r;
public ArrayList<String> letterCombinations(String digits) {
// Start typing your Java solution below
// DO NOT write main() function
r=new ArrayList<String>();
combine("",digits);
return r;
}
public String[] letter(char d) {
switch(d) {
case '2': return new String[]{"a","b","c"};
case '3': return new String[]{"d","e","f"};
case '4': return new String[]{"g","h","i"};
case '5': return new String[]{"j","k","l"};
case '6': return new String[]{"m","n","o"};
case '7': return new String[]{"p","q","r","s"};
case '8': return new String[]{"t","u","v"};
case '9': return new String[]{"w","x","y","z"};
}
return new String[1];
}
public void combine(String current,String rest) {
if(rest.equals("")) {
r.add(current);
return;
}
String[] l=letter(rest.charAt(0));
for(int i=0;i<l.length;i++) {
combine(current+l[i],rest.substring(1,rest.length()));
}
}
}