-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDigital_KeyPad.cpp
75 lines (56 loc) · 1.44 KB
/
Digital_KeyPad.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
70
71
72
73
74
75
// Given a classic mobile phone, and the key pad of the phone looks like below.
// 1 2 3
// abc def
// 4 5 6
// ghi jkl mno
// 7 8 9
// pqrs tuv wxyz
// * 0 #
// You are given a string S contains digits between [2-9] only,
// For example: S = "2", then the possible words are "a", "b", "c".
// Now your task is to find all possible words that the string S could represent.
// and print them in a lexicographical order.
// Input Format:
// -------------
// A string S, consist of digits [2-9]
// Output Format:
// --------------
// Print the list of words in lexicographical order.
// Sample Input-1:
// ---------------
// 2
// Sample Output-1:
// ----------------
// [a, b, c]
// Sample Input-2:
// ---------------
// 24
// Sample Output-2:
// ----------------
// [ag, ah, ai, bg, bh, bi, cg, ch, ci]
#include<bits/stdc++.h>
using namespace std;
vector<string> res;
unordered_map<char,string> m={{'2',"abc"},{'3',"def"},{'4',"ghi"},{'5',"jkl"},{'6',"mno"},{'7',"pqrs"},{'8',"tuv"},{'9',"wxyz"}};
void solve(string &digits,string &x,int ind){
if(ind==digits.size()){
res.push_back(x);
return;
}
string s=m[digits[ind]];
for(int i=0;i<s.size();i++){
x=x+s[i];
solve(digits,x,ind+1);
x.pop_back();
}
}
int main(){
int n;
cin>>n;
string s=to_string(n);
string x="";
solve(s,x,0);
for(auto i:res){
cout<<i<<" ";
}
}