c++ search solution


  • 0
    H
    class Solution {
    public:
        vector<string> str = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        vector<string> ans;
        void dfs(string digits, int idx, string cur) {
            if(idx == digits.size()) {
                ans.push_back(cur);
                return;
            } 
            string s = str[digits[idx] - '2'];
            for(int i = 0; i < s.size(); i ++)
               dfs(digits, idx+1, cur+s[i]);     
        }
        
        vector<string> letterCombinations(string digits) {
            if(digits.size() == 0) return ans;
            dfs(digits, 0, "");
            return ans;
        }
    };
    

Log in to reply
 

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