Short and concise 0ms C++ solution


  • 0
    W
    class Solution {
    public:
        vector<string> letterCombinations(string digits) {
            vector<vector<string>> res;
            if(digits.size()==0)    return vector<string>();
            vector<string> help = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
            res.push_back(vector<string>(1, ""));
            for(int i=0;i<digits.size();i++){
                res.push_back(vector<string>());
                for(auto s : res[i]){
                    for(auto c : help[digits[i]-'0'])
                        res[i+1].push_back(s + c);
                }
            }
            return res[digits.size()];
        }
    };
    

Log in to reply
 

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