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.
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.
解题思路:递归
class Solution {
public:
void innerLC(string &digits, int i, string &str, vector<string> &vs, map<char, string> &mc){
int n = digits.size();
if(n == i){
vs.push_back(str);
return;
}
for(int k = 0; k < mc[digits[i]].size(); k++){
str.push_back(mc[digits[i]][k]);
innerLC(digits, i + 1, str, vs, mc);
str.pop_back();
}
}
vector<string> letterCombinations(string digits) {
map<char, string> mc;
mc['1'] = "" ; mc['2'] = "abc"; mc['3'] = "def"; mc['4'] = "ghi";
mc['5'] = "jkl"; mc['6'] = "mno"; mc['7'] = "pqrs"; mc['8'] = "tuv";
mc['9'] = "wxyz"; mc['0'] = " ";
vector<string> vs;
string str;
innerLC(digits, 0, str, vs, mc);
return vs;
}
};