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:
void dfs(string table[],vector<string>&vec,int dep, int max, string &s, string cur){
string str = table[s.at(dep) - '2'];
for(int i = 0; i < str.length(); i++){
string next = cur + str.at(i);
if(dep == max - 1){
vec.push_back(next);
}
else{
dfs(table,vec,dep + 1, max, s, next);
}
}
}
vector<string> letterCombinations(string digits) {
vector<string>vec;
if(digits.length() <= 0){
vec.push_back("");
return vec;
}
string table[8] = {"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
dfs(table,vec,0,digits.size(),digits,"");
return vec;
}
};