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
| class Solution { public: vector<string> letterCombinations(string digits) { if (digits.empty()) return {};
doit(digits, {});
return ret; }
private: void doit(string digits, string const& str) { if (digits.size() == 0) { ret.emplace_back(str); return; }
vector<char>& charset = dict[int(digits[0]) - 48];
for (int i = 0; i < charset.size(); ++i) { doit({digits.begin() + 1, digits.end()}, str + charset[i]); } }
map<int, vector<char>> dict = { {2, {'a', 'b', 'c'}}, {3, {'d', 'e', 'f'}}, {4, {'g', 'h', 'i'}}, {5, {'j', 'k', 'l'}}, {6, {'m', 'n', 'o'}}, {7, {'p', 'q', 'r', 's'}}, {8, {'t', 'u', 'v'}}, {9, {'w', 'x', 'y', 'z'}} };
vector<string> ret; };
|