-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy path17.cpp
37 lines (32 loc) · 1 KB
/
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
37
/*
Problem 17. 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.
https://leetcode.com/problems/letter-combinations-of-a-phone-number/
*/
class Solution {
public:
string mp[10] = {
"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"
};
void helper(int st, string digits, vector<string>& ans) {
if(st == digits.length()) {
ans.push_back(digits);
return;
}
char temp = digits[st];
string vals = mp[temp - '0'];
for(int i = 0; i < vals.length(); i++) {
digits[st] = vals[i];
helper(st+1, digits, ans);
}
digits[st] = temp;
}
vector<string> letterCombinations(string digits) {
vector<string> ans;
if(digits.length() > 0)
helper(0, digits, ans);
return ans;
}
};