-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathletter_combination_phone_number.cpp
58 lines (46 loc) · 1.36 KB
/
letter_combination_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
/****************************************************************************
File name: letter_combination_phone_number.cpp
Author: babajr
*****************************************************************************/
/*
Letter Combinations of a Phone Number.
*/
#include <bits/stdc++.h>
using namespace std;
void phone_pad(string processed, string un_processed)
{
if(un_processed.length() == 0)
{
cout << processed << endl;
return;
}
int digit = un_processed[0] - '0'; // converting char to number
// i.e. '2' --> 2
for(int i = (digit - 1) * 3; i < (digit * 3); i++)
{
char ch = (char) ('a' + i);
phone_pad(processed + ch, un_processed.substr(1));
}
}
int phone_pad_count(string processed, string un_processed)
{
if(un_processed.length() == 0)
{
return 1;
}
int digit = un_processed[0] - '0'; // converting char to number
// i.e. '2' --> 2
int count = 0;
for(int i = (digit - 1) * 3; i < (digit * 3); i++)
{
char ch = (char) ('a' + i);
count = count + phone_pad_count(processed + ch, un_processed.substr(1));
}
return count;
}
int main(void)
{
phone_pad("", "12"); // phone_pad(string processed, string un_processed)
printf("%d\n", phone_pad_count("", "12"));
return 0;
}