-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPracticeQs2.java
More file actions
45 lines (38 loc) · 1.21 KB
/
PracticeQs2.java
File metadata and controls
45 lines (38 loc) · 1.21 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
public class PracticeQs2 {
final static char[][] Keypad = {
{},
{},
{ 'a', 'b', 'c' },
{ 'd', 'e', 'f' },
{ 'g', 'h', 'i' },
{ 'j', 'k', 'l' },
{ 'm', 'n', 'o' },
{ 'p', 'q', 'r', 's' },
{ 't', 'u', 'v' },
{ 'w', 'x', 'y', 'z' }
};
public static void letterCombinations(String str) {
int len = str.length();
// If the string is empty print nothing
if (len == 0) {
System.out.println("");
return;
}
bfs(0, len, new StringBuilder(), str);
}
public static void bfs(int pos, int len, StringBuilder output, String input) {
// Base Condition
if (pos == len) {
System.out.println(output);
} else {
char[] letters = Keypad[Character.getNumericValue(input.charAt(pos))];
// loop from 0 character to last character
for (int i = 0; i < letters.length; i++) {
bfs(pos + 1, len, new StringBuilder(output).append(letters[i]), input);
}
}
}
public static void main(String[] args) {
letterCombinations("22");
}
}