Q17. Letter Combinations of a Phone Number
Last updated
Last updated
class Solution {
public:
void letterCombinations_helper(vector<string>&res, string digits, int idx, string path){
vector<string> phone = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
string str = phone[digits[idx]-'2'];
for(int i=0; i<str.size(); i++){
if(idx == digits.size() -1){
res.push_back(path+str[i]);
}else{
letterCombinations_helper(res, digits, idx+1, path+str[i]);
}
}
}
vector<string> letterCombinations(string digits) {
vector<string> res;
int idx = 0;
string path = "";
letterCombinations_helper(res, digits, idx, path);
return res;
}
};