Precise c++ solution..could it be better


  • 0
    L
    void letcomb(string &digits, vector<string> &res, string r, int i )
    {
        static const vector<string> v = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        
        if( i==digits.size()) {
            res.push_back(r);
            return;
        }
        int b= digits[i]-'0';
        
        for(int k=0; k<v[b].size(); k++) {
            letcomb(digits, res, r+v[b][k],i+1);
        }
    }
    vector<string> letterCombinations(string digits) {
         vector<string> res;
         if( digits.size() == 0) return res;
    
         letcomb(digits,res, "",0 );
         return res;

Log in to reply
 

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