Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class Solution {
private:
vector<string> dic_s = {"", "", "abc", "def", "ghi","jkl", "mno","pqrs", "tuv", "wxyz"};
vector<string> result;
public:
void dfs(const string &digits, string s, int pos){
int len = digits.size();
if(pos >= len){
return;
}
int subLen = dic_s[digits[pos] - '0'].size();
for(int i = 0; i< subLen; i++)
{
string dic_string = dic_s[digits[pos] - '0'];
string temp = s + dic_string[i];
if(pos == len - 1)
{
result.push_back(temp);
}
dfs(digits, temp, pos + 1);
}
}
vector<string> letterCombinations(string digits) {
dfs(digits, "", 0);
return result;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment