-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtelephone-numbers-to-possible-words.cpp
70 lines (61 loc) · 1.39 KB
/
telephone-numbers-to-possible-words.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
/**
* @file telephone-numbers-to-possible-words.cpp
* @author prakash (prakashsellathurai@gmail.com)
* @brief
Telephone keypads have letters on each numerical key. Write a program that
generates all possible words resulting from translating a given digit
sequence (e.g. 145345) into letters.
* @version 0.1
* @date 2021-08-11
*
* @copyright Copyright (c) 2021
*
*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int steps = 0;
void findCombination(
const vector<vector<char>> &keypad,
vector<int> numbers,
string result,
int n,
int index
){
steps++;
int digit;
int len;
if(index == n){
cout << result << endl;
return;
}
if(index > n) return;
digit = numbers[index];
len = keypad[digit].size();
for(int i=0;i<len;i++){
findCombination(keypad, numbers, result+keypad[digit][i], n, index+1);
}
}
int main(int argc, const char **argv) {
vector<vector<char>> keypad = {
{},
{},
{'A', 'B', 'C'},
{'D', 'E', 'F'},
{'G', 'H', 'I'},
{'J', 'K', 'L'},
{'M', 'N', 'O'},
{'P', 'Q', 'R', 'S'},
{'T', 'U', 'V'},
{'W', 'X', 'Y', 'Z'},
};
vector<int> numbers = {2, 3, 4};
int n = numbers.size();
string result = "";
int index = 0;
findCombination(keypad, numbers, result,n, index);
cout<<steps<<endl;
return 0;
}