-
Notifications
You must be signed in to change notification settings - Fork 41
/
LetterCombinationsofPhoneNumber.cpp
34 lines (31 loc) · 1.26 KB
/
LetterCombinationsofPhoneNumber.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
//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.
//A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
//LeetCode Url:https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/
//SOLUTION
class Solution {
private:
void letterCombinations(string digits, vector<string>& output, string &temp, vector<string>& pad, int index){
if(index == digits.size()){
output.push_back(temp);
return;
}
string value = pad[digits[index]-'0'];
for(int i=0; i<value.size(); i++){
temp.push_back(value[i]);
letterCombinations(digits, output, temp, pad, index+1);
temp.pop_back();
}
}
public:
vector<string> letterCombinations(string digits) {
if(digits.empty()){
return {};
}
vector<string> pad = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
vector<string> output;
string temp;
letterCombinations(digits, output, temp, pad, 0);
return output;
}
};