-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path17.cpp
36 lines (31 loc) · 971 Bytes
/
17.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
// 17. Letter Combinations of a Phone Number - https://leetcode.com/problems/letter-combinations-of-a-phone-number
#include "bits/stdc++.h"
using namespace std;
class Solution {
private:
vector<string> result, numbers = {
"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"
};
public:
void helper(int index, string cur_answer, string digits) {
if (index == digits.length()) {
result.emplace_back(cur_answer);
cout << cur_answer << endl;
return;
}
for (char digit : numbers[digits[index] - '0']) {
cur_answer.push_back(digit);
helper(index + 1, cur_answer, digits);
cur_answer.pop_back();
}
}
vector<string> letterCombinations(string digits) {
if (digits.empty()) { return result; }
helper(0, "", digits);
return result;
}
};
int main() {
ios::sync_with_stdio(false);
return 0;
}