-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0017-Letter_Combinations_of_a_Phone_Number.cpp
119 lines (105 loc) · 2.87 KB
/
0017-Letter_Combinations_of_a_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*******************************************************************************
* 0017-Letter_Combinations_of_a_Phone_Number.cpp
* Billy.Ljm
* 03 August 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/letter-combinations-of-a-phone-number/
*
* Given a string containing digits from 2-9 inclusive, return all possible
* letter combinations that the number could represent. Return the answer in any
* order.
*
* ===========
* My Approach
* ===========
* Since we have to generate each possible permutation, we have no choice but to
* iterate through each of them. We'll do this recursively, one digit at a time.
*
* This has a time complexity of O(3^n * n), and a space complexity of O(3^n),
* where n is the length of the string.
******************************************************************************/
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (int i = 0; i < v.size(); i++) {
os << v[i] << ",";
}
os << "\b]";
return os;
}
/**
* Solution
*/
class Solution {
private:
map<char, vector<char>> mapp;
public:
Solution() {
// telephone button mappings
mapp.insert({ '2', vector<char>({'a', 'b', 'c'}) });
mapp.insert({ '3', vector<char>({'d', 'e', 'f'}) });
mapp.insert({ '4', vector<char>({'g', 'h', 'i'}) });
mapp.insert({ '5', vector<char>({'j', 'k', 'l'}) });
mapp.insert({ '6', vector<char>({'m', 'n', 'o'}) });
mapp.insert({ '7', vector<char>({'p', 'q', 'r', 's'}) });
mapp.insert({ '8', vector<char>({'t', 'u', 'v'}) });
mapp.insert({ '9', vector<char>({'w', 'x', 'y', 'z'}) });
mapp.insert({ '0', vector<char>({' '}) });
}
vector<string> letterCombinations(string digits) {
// edge case
if (digits.size() == 0) return vector<string>(0);
// base case
if (digits.size() == 1) {
vector<string> out;
for (char c : mapp[digits.back()]) {
out.push_back(string(1, c));
}
return out;
}
// general case
else {
// recrurse preceeding digit
vector<string> prev = letterCombinations(digits.substr(0, digits.size() - 1));
// add last digit
vector<string> out;
for (char c : mapp[digits.back()]) {
for (string s : prev) {
out.push_back(s + c);
}
}
return out;
}
}
};
/**
* Test cases
*/
int main(void) {
Solution sol;
string digits;
// test case 1
digits = "23";
std::cout << "letterCombinations(" << digits << ") = ";
std::cout << sol.letterCombinations(digits) << std::endl;
// test case 2
digits = "";
std::cout << "letterCombinations(" << digits << ") = ";
std::cout << sol.letterCombinations(digits) << std::endl;
// test case 3
digits = "2";
std::cout << "letterCombinations(" << digits << ") = ";
std::cout << sol.letterCombinations(digits) << std::endl;
return 0;
}