-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnemonics_of_phone_number.cpp
62 lines (46 loc) · 1.48 KB
/
mnemonics_of_phone_number.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
//compute all mnemonics of phone number
//https://ide.geeksforgeeks.org/vzLQgnt2tq
#include <bits/stdc++.h>
using namespace std;
void recurviseSolve(string digits, string cycle, vector<string>& combinations, int start, int n, string mapper[10]){
if(start == n){
combinations.push_back(cycle);
return;
}
int first = digits[start] - '0';
int len = mapper[first].length();
for(int i=0; i<len; i++){
recurviseSolve(digits, cycle + mapper[first][i], combinations, start + 1, n , mapper);
}
}
/* Iterative approach:
vector<string> letterCombinations(string digits) {
vector<string> res;
string charmap[10] = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
res.push_back("");
for (int i = 0; i < digits.size(); i++)
{
vector<string> tempres;
string chars = charmap[digits[i] - '0'];
for (int c = 0; c < chars.size();c++)
for (int j = 0; j < res.size();j++)
tempres.push_back(res[j]+chars[c]);
res = tempres;
}
return res;
}
*/
int main() {
//int y; cin>>y;
string digits; cin>>digits;
/*for(int i=0; i<y; i++)
cin>>digits[i];
*/
string cycle = "";
vector<string> combinations;
string mapper[10] = {" ", " ", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
int start = 0, n = digits.length();
recurviseSolve(digits, cycle, combinations, start, n, mapper);
for(string x: combinations) cout<<x<<" ";
return 0;
}