-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathPossible words from Phone digits.cpp
57 lines (50 loc) · 1.19 KB
/
Possible words from Phone digits.cpp
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
/*
Problem Statement : https://practice.geeksforgeeks.org/problems/possible-words-from-phone-digits/0/?track=SP-Arrays%20and%20Searching
Asked in : Amazon, Flipkart
Code By:
Abhinav,
CSE, NIT P
(Masters_Abh on codechef, codeforces, hackerrank and Spoj)
*/
#include <bits/stdc++.h>
using namespace std;
#define lint long long int
map<lint,vector<char> > m;
void chk(lint a[],lint k,lint n,string s){ // Idea is to use a map storing chars for each no. and then use recursion to print all possible strings
if(k==n){ // resulting from the characters of the input numbers in their order.
cout<<s<<" ";
return;
}
for(lint i=0;i<m[a[k]].size();i++){
chk(a,k+1,n,s+m[a[k]][i]);
}
}
int main() {
lint t;
cin>>t;
lint x=2;
char s='a';
while(x<10){
lint l=3;
if(x==7 || x==9){
l=4;
}
for(lint i=0;i<l;i++){
m[x].push_back(s);
s++;
}
x++;
}
while(t--){
lint n;
cin>>n;
lint a[n];
for(lint i=0;i<n;i++){
cin>>a[i];
}
string h="";
chk(a,0,n,h);
cout<<endl;
}
return 0;
}