-
Notifications
You must be signed in to change notification settings - Fork 1
/
36-possible-words-from-phone-digits.cpp
69 lines (51 loc) · 1.27 KB
/
36-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
58
59
60
61
62
63
64
65
66
67
68
69
//Initial Template for C++
#include <bits/stdc++.h>
#include <string>
using namespace std;
// } Driver Code Ends
//User function Template for C++
class Solution
{
public:
vector<string> sol;
vector<string> record = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
void helper(int a[], int n, int index, string s)
{
if(index == n)
{
sol.push_back(s);
return;
}
for(int i = 0; i < record[a[index] - 2].size(); ++i)
{
s += record[a[index] - 2][i];
helper(a, n, index + 1, s);
s.pop_back();
}
return;
}
vector<string> possibleWords(int a[], int N)
{
string s = "";
helper(a, N, 0, s);
return sol;
}
};
// { Driver Code Starts.
int main() {
int T;
cin >> T; //testcases
while(T--){ //while testcases exist
int N;
cin >> N; //input size of array
int a[N]; //declare the array
for(int i =0;i<N;i++){
cin >> a[i]; //input the elements of array that are keys to be pressed
}
Solution obj;
vector <string> res = obj.possibleWords(a,N);
for (string i : res) cout << i << " ";
cout << endl;
}
return 0;
} // } Driver Code Ends