-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPossibleWordFromPhoneDigit.java
More file actions
54 lines (39 loc) · 1.6 KB
/
PossibleWordFromPhoneDigit.java
File metadata and controls
54 lines (39 loc) · 1.6 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
import java.util.ArrayList;
import java.util.Scanner;
import java.lang.StringBuilder;
class PossibleWordFromPhoneDigit {
static String str[]={ " " , " " ,"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the lenth of the array: ");
int n = sc.nextInt();
System.out.println("Enter " + n + " digits present in the array: ");
int arr[] = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
ArrayList<String> res=possibleWords(arr, n);
System.out.println("\n\n Result :");
for (String string : res) {
System.out.print(string+" ");
}
}
public static ArrayList<String> possibleWords(int a[], int N) {
ArrayList<String> possibleWord=new ArrayList<String>();
StringBuffer msg=new StringBuffer();
getResults(0,a,msg,possibleWord);
return possibleWord;
}
public static void getResults(int index, int arr[], StringBuffer msg, ArrayList<String>possibleWord) {
if(index==arr.length){
possibleWord.add(msg.toString());
System.out.println("Inside of the result mathod, the output is: "+ msg);
return;
}
for(int i=0;i<str[arr[index]].length();i++){
msg.append(str[arr[index]].charAt(i));
getResults(index+1, arr, msg, possibleWord);
msg.deleteCharAt(index);
}
}
}