-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoneKeypadCombinations.java
More file actions
75 lines (59 loc) · 1.93 KB
/
PhoneKeypadCombinations.java
File metadata and controls
75 lines (59 loc) · 1.93 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//CHECK THIS FILE FOR TEMPLATE :- Keypad.png
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class PhoneKeypadCombinations {
// Keypad mapping
static Map<Integer, String> keypad = new HashMap<>();
static {
keypad.put(2, "abc");
keypad.put(3, "def");
keypad.put(4, "ghi");
keypad.put(5, "jkl");
keypad.put(6, "mno");
keypad.put(7, "pqrs");
keypad.put(8, "tuv");
keypad.put(9, "wxyz");
}
// Recursive function to find all combinations
public static void findCombinations(int[] digits, int index, String current, List<String> result) {
if (index == digits.length) {
result.add(current);
return;
}
int digit = digits[index];
String letters = keypad.get(digit);
if (letters != null) {
for (char letter : letters.toCharArray()) {
findCombinations(digits, index + 1, current + letter, result);
}
} else {
findCombinations(digits, index + 1, current, result);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input number of digits
System.out.print("Enter the number of digits: ");
int n = sc.nextInt();
int[] digits = new int[n];
// Take inputs individually
System.out.println("Enter the digits:");
for (int i = 0; i < n; i++) {
digits[i] = sc.nextInt();
}
List<String> result = new ArrayList<>();
// Add an empty string for blank possibility
result.add("");
if (digits.length > 0) {
findCombinations(digits, 0, "", result);
}
// Output the result
for (String combination : result) {
System.out.println(combination);
}
sc.close();
}
}