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> table = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    vector<string> letterCombinations(string digits) {
    	vector<string> result;
    	int len = digits.length();
    
    	if(0 == len)
    	{
    		result.push_back("");
    		return result;
    	}
    	
    	int index = digits[0] - '2';
    	vector<string> tmp= letterCombinations(digits.substr(1));
    	for(int i = 0;i < table[index].size();++i)
    	{
    		for(int j = 0;j < tmp.size();++j)
    		{
    			string tmp1;
    			tmp1.append(1,table[index][i]);
    			tmp1 += tmp[j];
    			result.push_back(tmp1);
    		}
    	}
    	
    	return result;
    }
};

 

posted on 2014-12-30 19:56  风云逸  阅读(74)  评论(0)    收藏  举报