-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPhonePad.java
More file actions
36 lines (34 loc) · 1.1 KB
/
PhonePad.java
File metadata and controls
36 lines (34 loc) · 1.1 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
package strings;
import java.util.ArrayList;
public class PhonePad {
public static void main(String[] args) {
// pad("","12");
System.out.println(padRet("","12"));
}
// not understood
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;
}
}