CPP recursive DFS


  • 0
    B
        vector<string> letterCombinations(string digits) {
            
            static string smap[] = {
                    "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"
            };
            vector<string> result;
            function<void(size_t, string)> dfs = [&](size_t i = 0, string path = "") {
                if(i == digits.size()) result.push_back(path);
                else {
                    int imap = digits[i] - '2';
                    for (char c: smap[imap]) {
                        dfs(i + 1, path + c);
                    }
                }
            };
            if(!digits.empty()) dfs(0, "");
            return result;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.