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:
string num[10];
vector<string> ans;
int digit[10];
char tmp[10];
int n;
void dfs(int depth)
{
if(depth == n)
{
string s(tmp);
ans.push_back(s);
}
for(int i=0; i<num[digit[depth]].size(); i++)
{
tmp[depth] = num[digit[depth]][i];
dfs(depth+1);
}
}
vector<string> letterCombinations(string digits)
{
num[0] = "";
num[1] = "";
num[2] = "abc";
num[3] = "def";
num[4] = "ghi";
num[5] = "jkl";
num[6] = "mno";
num[7] = "pqrs";
num[8] = "tuv";
num[9] = "wxyz";
n = digits.length();
if(n == 0)
return ans;
for(int i=0; i<n; i++)
digit[i] = digits[i] - '0';
dfs(0);
return ans;
}
};