diff --git a/Letter Combinations of a Phone Number b/Letter Combinations of a Phone Number new file mode 100644 index 0000000..fe1334a --- /dev/null +++ b/Letter Combinations of a Phone Number @@ -0,0 +1,24 @@ +class Solution { +public: + const vector pad = { + "", "", "abc", "def", "ghi", "jkl", + "mno", "pqrs", "tuv", "wxyz" + }; + + vector letterCombinations(string digits) { + if (digits.empty()) return {}; + vector result; + result.push_back(""); + + for(auto digit: digits) { + vector tmp; + for(auto candidate: pad[digit - '0']) { + for(auto s: result) { + tmp.push_back(s + candidate); + } + } + result.swap(tmp); + } + return result; + } +};