Short and easy C++ solution


  • 0
    A
    class Solution {
    public:
        vector<string> str;
        vector<string> res;
       
        vector<string> letterCombinations(string digits) {
            if(digits.size()<1){
                return res;
            }    
            int i;
            str.resize(digits.size());
            vector<string> c(256);
            c['2'] = "abc"; c['3'] = "def";
            c['4'] = "ghi"; c['5'] = "jkl";
            c['6'] = "mno"; c['7'] = "pqrs";
            c['8'] = "tuv"; c['9'] = "wxyz";
            for(i=0;i<digits.size();i++){
                str[i]=c[digits[i]];
            }
            string t=digits;
            clc(0,digits,t);
            return res;
        }
        void clc(int i,string digits,string t){
            if(i==digits.size()){
                res.push_back(t);
                return;
            }else{
                for(int j=0;j<str[i].size();j++){
                    t[i]=str[i][j];
                    clc(i+1,digits,t);
                }  
            }
        }
        
        
    };
    

Log in to reply
 

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