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




近期评论