天天看點

leetcode---letter-combinations-of-a-phone-number---dfs

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:
    void dfs(int dep, string tmp, vector<string> &ans, int n, string digits, string s[])
    {
        if(dep >= n)
        {
            ans.push_back(tmp);
        }

        int k = digits[dep] - '0';
        for(int j=; j<s[k].size(); j++)
            dfs(dep+, tmp+s[k][j], ans, n, digits, s);
    }

    vector<string> letterCombinations(string digits) 
    {
        string s[] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        vector<string> ans;
        int n = digits.size();
        string tmp = "";
        dfs(, tmp, ans, n, digits, s);
        return ans;
    }
};