-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhonePad.java
More file actions
51 lines (44 loc) · 1.47 KB
/
PhonePad.java
File metadata and controls
51 lines (44 loc) · 1.47 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
import java.util.ArrayList;
public class PhonePad {
public static void main(String[] args) {
System.out.println(padRet("", "12").size());
System.out.println(padCount("", "12"));
}
static void pad(String p, String up) {
if (up.isEmpty()) {
System.out.println(p);
return;
}
int digit = up.charAt(0) - '0'; // this will convert '2' into 2
for (int i = (digit - 1) * 3; i < digit * 3; i++) {
char ch = (char) ('a' + i);
pad(p + ch, up.substring(1));
}
}
static ArrayList<String> padRet(String p, String up) {
if (up.isEmpty()) {
ArrayList<String> list = new ArrayList<>();
list.add(p);
return list;
}
int digit = up.charAt(0) - '0'; // this will convert '2' into 2
ArrayList<String> list = new ArrayList<>();
for (int i = (digit - 1) * 3; i < digit * 3; i++) {
char ch = (char) ('a' + i);
list.addAll(padRet(p + ch, up.substring(1)));
}
return list;
}
static int padCount(String p, String up) {
if (up.isEmpty()) {
return 1;
}
int count = 0;
int digit = up.charAt(0) - '0'; // this will convert '2' into 2
for (int i = (digit - 1) * 3; i < digit * 3; i++) {
char ch = (char) ('a' + i);
count = count + padCount(p + ch, up.substring(1));
}
return count;
}
}