Clean iterative C++


  • 0
    M
        vector<string> letterCombinations(string digits) {
            if (digits.empty()) return {};
    
            static const string M[] = {
                "",    "",     "abc",
                "def", "ghi",  "jkl",
                "mno", "pqrs", "tuv",
                "wxyz"
            };
    
            vector<string> res(1), tmp;
    
            for (auto ch : digits) {
                tmp.clear();
    
                for (auto ch : M[ch - '0'])
                    for (auto str : res)
                        tmp.push_back(str + ch);
    
                res.swap(tmp);
            }
                
            return res;
        }
    

Log in to reply
 

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