-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoneLetterCombination.java
More file actions
58 lines (57 loc) · 1.29 KB
/
PhoneLetterCombination.java
File metadata and controls
58 lines (57 loc) · 1.29 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.ArrayList;
public class PhoneLetterCombination {
public static void main(String[] args) {
System.out.println(combination("","9"));
}
static ArrayList<String> combination(String p, String up)
{
if(up.length()==0)
{
ArrayList<String> ans = new ArrayList<String>();
ans.add(p);
return ans;
}
char ch = up.charAt(0);
String letter = "";
if(ch=='2')
{
letter = "abc";
}
else if(ch=='3')
{
letter = "def";
}
else if(ch=='4')
{
letter = "ghi";
}
else if(ch=='5')
{
letter = "jkl";
}
else if(ch=='6')
{
letter = "mno";
}
else if(ch=='7')
{
letter = "pqrs";
}
else if(ch=='8')
{
letter = "tuv";
}
else if(ch=='9')
{
letter = "wxyz";
}
//System.out.println(letter);
ArrayList<String> ans = new ArrayList<String>();
for(int i=0;i<letter.length();i++)
{
char c = letter.charAt(i);
ans.addAll(combination(p+c, up.substring(1)));
}
return ans;
}
}