Skip to content

Commit 2162915

Browse files
authored
Merge pull request #515 from paurush11/master
Possible Words From Phone Digits
2 parents 7299d00 + ec81ded commit 2162915

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
Possible Words From Phone Digits
3+
Medium Accuracy: 51.47% Submissions: 11922 Points: 4
4+
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
5+
6+
7+
Example 1:
8+
9+
Input: N = 3, a[] = {2, 3, 4}
10+
Output:
11+
adg adh adi aeg aeh aei afg afh afi
12+
bdg bdh bdi beg beh bei bfg bfh bfi
13+
cdg cdh cdi ceg ceh cei cfg cfh cfi
14+
Explanation: When we press 2,3,4 then
15+
adg, adh, adi, ... cfi are the list of
16+
possible words.
17+
Example 2:
18+
19+
Input: N = 3, a[] = {3, 4, 5}
20+
Output:
21+
dgj dgk dgl dhj dhk dhl dij dik dil
22+
egj egk egl ehj ehk ehl eij eik eil
23+
fgj fgk fgl fhj fhk fhl fij fik fil
24+
Explanation: When we press 3,4,5 then
25+
dgj, dgk, dgl, ... fil are the list of
26+
possible words.
27+
Your Task:
28+
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
29+
30+
Expected Time Complexity: O(4N * N).
31+
Expected Auxiliary Space: O(N).
32+
33+
Constraints:
34+
1 ≤ N ≤ 10
35+
2 ≤ a[i] ≤ 9
36+
*/
37+
38+
//Initial Template for C++
39+
40+
#include <bits/stdc++.h>
41+
#include <string>
42+
43+
using namespace std;
44+
45+
46+
// } Driver Code Ends
47+
//User function Template for C++
48+
49+
class Solution
50+
{
51+
public:
52+
//Function to find list of all words possible by pressing given numbers.
53+
54+
55+
void formstring(int a[], int N, string s, vector<string>&v, int i,map<int, vector<char>>m){
56+
if(i == N){
57+
v.push_back(s);
58+
return;
59+
}
60+
int pos = a[i];
61+
for(auto j : m[pos]){
62+
formstring(a,N,s+j,v,i+1,m);
63+
}
64+
}
65+
vector<string> possibleWords(int a[], int N)
66+
{
67+
map<int, vector<char>>m;
68+
int k = 0;
69+
for(int i = 2;i<=6;i++){
70+
int j = 0;
71+
while(j<3){
72+
m[i].push_back('a'+ j + k);
73+
j++;
74+
}
75+
k+=3;
76+
}
77+
m[7] = {'p','q','r','s'};
78+
m[8] = {'t','u','v'};
79+
m[9] = {'w','x','y','z'};
80+
81+
vector<string>ans;
82+
formstring(a,N,"",ans,0,m);
83+
return ans;
84+
}
85+
};
86+
87+
88+
// { Driver Code Starts.
89+
90+
int main() {
91+
92+
int T;
93+
94+
cin >> T; //testcases
95+
96+
while(T--){ //while testcases exist
97+
int N;
98+
99+
cin >> N; //input size of array
100+
101+
int a[N]; //declare the array
102+
103+
for(int i =0;i<N;i++){
104+
cin >> a[i]; //input the elements of array that are keys to be pressed
105+
}
106+
107+
Solution obj;
108+
109+
vector <string> res = obj.possibleWords(a,N);
110+
for (string i : res) cout << i << " ";
111+
cout << endl;
112+
}
113+
114+
return 0;
115+
} // } Driver Code Ends

0 commit comments

Comments
 (0)