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"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
class Solution {
public:
vector<string> letterCombinations(string digits) {
string path;
vector<string> res;
int step = 0;
dfs(step, path, res, digits);
return res;
}
void dfs(int step, string &path, vector<string> &res, const string &digits)
{
const static string strT[10] = {"", "", "abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
if(step == digits.size())
{
res.push_back(path);
return;
}
for(int i = 0; i < strT[digits[step]-'0'].size(); i++)
{
path.push_back(strT[digits[step]-'0'][i]);
dfs(step+1, path, res, digits);
path.pop_back();
}
}
};