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:
Solution()
{
mat['2'] = "abc";
mat['3'] = "def";
mat['4'] = "ghi";
mat['5'] = "jkl";
mat['6'] = "mno";
mat['7'] = "pqrs";
mat['8'] = "tuv";
mat['9'] = "wxyz";
}
std::vector<std::string> letterCombinations(std::string digits) {
std::vector<std::string> result;
if ( digits.size() == 0 )
{
result.push_back("");
return result;
}
std::vector<std::string> temp = letterCombinations(digits.substr(1));
for(int j = 0; j < mat[digits[0]].size(); j ++)
for(int t = 0; t < temp.size(); t ++ )
result.push_back(mat[digits[0]][j] + temp[t]);
return result;
}
private:
std::unordered_map<char,std::string> mat;
};