Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below.

fuck

1
2
Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Note:

Although the above answer is in lexicographical order, your answer could be in any order you want.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
private:
vector<string>mp = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public:
vector<string> letterCombinations(string digits) {
vector<string> res;
if(digits.empty())
return res;
res.push_back("");
for(int i = 0; digits[i]; i++){
vector<string> tmp;
for(int j = 0; mp[digits[i]-'0'][j]; j++)
for(int k = 0; k < res.size(); k++)
tmp.push_back(res[k] + mp[digits[i] - '0'][j]);
res = tmp;
}
return res;
}
};