Last active
April 25, 2019 08:38
-
-
Save bunnyadad/69d09d112cb86081e81ae8e79df59b52 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution | |
{ | |
public: | |
vector<string> letterCombinations(string digits) | |
{ | |
if (digits.empty()) return{}; | |
static vector<string>dict = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }; | |
vector<string> output; | |
CombineLetterDFS(digits, dict, "", output); | |
return output; | |
} | |
private: | |
void CombineLetterDFS(const string& digits, const vector<string>& dict, string out, vector<string>& output) | |
{ | |
if (digits.empty()) | |
{ | |
output.emplace_back(out); | |
return; | |
} | |
for (auto letter : dict[digits[0] - '0']) | |
{ | |
CombineLetterDFS(string(digits.begin() + 1, digits.end()), dict, out + letter, output); | |
} | |
} | |
}; |
second version
Runtime 8 ms
Memory 8.7 MB
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
first version
Runtime 4 ms
Memory 9.2 MB