forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths1.cpp
More file actions
26 lines (26 loc) · 758 Bytes
/
s1.cpp
File metadata and controls
26 lines (26 loc) · 758 Bytes
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
// OJ: https://leetcode.com/problems/letter-combinations-of-a-phone-number/
// Author: github.com/lzl124631x
// Time: O(4^N)
// Space: O(N)
class Solution {
vector<string> m{"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
vector<string> ans;
void dfs(string &digits, int start, string &str) {
if (start == digits.size()) {
ans.push_back(str);
return;
}
for (char c : m[digits[start] - '2']) {
str.push_back(c);
dfs(digits, start + 1, str);
str.pop_back();
}
}
public:
vector<string> letterCombinations(string digits) {
if (digits.empty()) return {};
string str;
dfs(digits, 0, str);
return ans;
}
};