-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion10.java
More file actions
49 lines (38 loc) · 1.19 KB
/
Recursion10.java
File metadata and controls
49 lines (38 loc) · 1.19 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
public class Recursion10 {
// Keypad mapping array
static String[] keypad = {
".", // 0
"abc", // 1
"def", // 2
"ghi", // 3
"jkl", // 4
"mno", // 5
"pqrs", // 6
"tu", // 7
"vwx", // 8
"yz" // 9
};
// Recursive function to generate combinations
public static void printCombination(String str, int index, String combination) {
// Base case: If all digits processed
if (index == str.length()) {
System.out.println(combination);
return;
}
// Current digit
char currChar = str.charAt(index);
// Convert char to integer
int digit = currChar - '0';
// Get corresponding letters from keypad
String mapping = keypad[digit];
// Loop through all letters of that digit
for (int i = 0; i < mapping.length(); i++) {
// Recursive call with next digit
printCombination(str, index + 1, combination + mapping.charAt(i));
}
}
public static void main(String[] args) {
String input = "23";
printCombination(input, 0, "");
}
}