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 38 39 40 41
| class Solution { public:
vector<string> res; string temp;
unordered_map<char, string> table{ {'0', " "}, {'1',"*"}, {'2', "abc"}, {'3',"def"}, {'4',"ghi"}, {'5',"jkl"}, {'6',"mno"}, {'7',"pqrs"},{'8',"tuv"}, {'9',"wxyz"}};
void dfs(string& digits, int curpos){
if(curpos == digits.size()){
res.push_back(temp); return; }
char now_str = digits[curpos]; for(int i = 0; i < table[now_str].size(); i++){
temp += table[now_str][i]; dfs(digits, curpos + 1);
temp.pop_back(); }
}
vector<string> letterCombinations(string digits) {
if(digits.size() == 0) return {};
dfs(digits, 0);
return res; } };
|