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 str[] = {"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
int n = digits.size();
vector<string> res;
if(!n)
return res;
res.resize(1);
for(int i = 0; i < n; i++)
{
for(int j = res.size()-1; j >= 0; j--)
{
string s = str[digits[i]-'2'];
for(int k = s.size()-1; k>=0; k--)
{
if(k)
res.push_back(res[j]+s[k]);
else
res[j] += s[k];
}
}
}
return res;
}
};