Skip to content

Instantly share code, notes, and snippets.

@Onaiplee
Last active August 29, 2015 14:03
Show Gist options
  • Save Onaiplee/96743c715322f6207ddb to your computer and use it in GitHub Desktop.
Save Onaiplee/96743c715322f6207ddb to your computer and use it in GitHub Desktop.
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> mmap({"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"});
string buf;
vector<string> result;
dfs(0, mmap, buf, digits, result);
return result;
}
void dfs(int level, vector<string> &mmap, string &buf, string &digits, vector<string> &result) {
if (level == digits.size()) {
result.push_back(buf);
return;
}
for (int i = 0; i < mmap[(digits[level] - '0')-2].size(); ++i) {
buf.push_back(mmap[(digits[level]- '0')-2][i]);
dfs(level + 1, mmap, buf, digits, result);
buf.pop_back();
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment