-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17_phoneKeypd.cpp
32 lines (28 loc) · 924 Bytes
/
17_phoneKeypd.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
class Solution {
private:
void solve(string digit, string output, int index, vector<string>& ans, string mapping[] ) {
//base case
if(index >= digit.length()) {
ans.push_back(output);
return;
}
int number = digit[index] - '0';
string value = mapping[number];
for(int i=0; i<value.length(); i++) {
output.push_back(value[i]);
solve(digit, output, index+1, ans, mapping);
output.pop_back();
}
}
public:
vector<string> letterCombinations(string digits) {
vector<string> ans;
if(digits.length()==0)
return ans;
string output;
int index = 0;
string mapping[10] = {"", "", "abc", "def", "ghi", "jkl","mno","pqrs","tuv","wxyz"};
solve(digits, output, index, ans, mapping);
return ans;
}
};