Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
Input:Digit string "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> dict = vector<string>({"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"});
string comb;
vector<string> res;
if (digits.size()<=0)
return res;
help(digits,0,comb,res,dict);
return res;
}
void help(string s, int i, string &comb, vector<string> &res, vector<string> &dict){
if (i>=s.size()){
res.push_back(comb);
return;
}
string temp = dict[s[i]-'0'];
if (temp.size()==0)
help(s,i+1,comb,res,dict);
else{
for (int j=0;j<temp.size();j++){
comb.push_back(temp[j]);
help(s,i+1,comb,res,dict);
comb.pop_back();
}
}
}
};
No comments:
Post a Comment