Letter Combinations of a Phone Number


  • 0
    S

    class Solution {
    public:
    unordered_map<char,string> HashMap = { {'1',""}, {'2', "abc"},{'3', "def"},{'4', "ghi"},{'5', "jkl"},
    {'6',"mno"},{'7',"pqrs"},{'8',"tuv"},{'9',"wxyz"},{'0',""}};

    void letterletterCombinationsHelper(string &d, int digit,string partial,vector<string> &v){
     
        if(partial.size() == d.size())
        {
            v.emplace_back(partial);
        }
        else{
        for (const char &c: HashMap[d[digit]]){
            letterletterCombinationsHelper(d,digit+1,partial+c,v);
          }
              
        }
    }
    
    vector<string> letterCombinations(string digits) {
        vector<string> s; 
        string partial;
        if(digits == ""){
            return {};
        }
        letterletterCombinationsHelper(digits,0,partial,s);
        return s;
    }
    

    };


  • 0
    D
    class Solution {
    public:
        vector<string> letterCombinations(string digits) 
        {
            static const vector<vector<char>> v_map
                                      {
                                       {}, 
                                       {}, 
                                       {'a', 'b', 'c'},
                                       {'d', 'e', 'f'},
                                       {'g', 'h', 'i'},
                                       {'j', 'k', 'l'},
                                       {'m', 'n', 'o'},
                                       {'p', 'q', 'r', 's'},
                                       {'t', 'u', 'v'},
                                       {'w', 'x', 'y', 'z'}
                                      };
            vector<string> rt;
            auto len = digits.length();
            if (0 == len)
                return rt;
            for (std::size_t i = 0; i < digits.size(); ++i)
            {
                const auto& v = v_map[digits[i] - '0'];
                if (rt.empty())
                {
                    for (std::size_t j = 0; j < v.size(); ++j)
                    {
                        string str = "";
                        str += v[j];
                        rt.push_back(str);
                    }
                }
                else
                {
                    if (!v.empty())
                    {
                        auto ori_size = rt.size();
                        for (std::size_t j = 1; j < v.size(); ++j)
                        {
                            for (std::size_t k = 0; k < ori_size; ++k)
                            {
                                rt.push_back(rt[k] + v[j]);
                            }
                        }
                        for (std::size_t k = 0; k < ori_size; ++k)
                        {
                            rt[k] += v[0];
                        }
                    }
                }
            }
            return rt;
        }
    };
    

Log in to reply
 

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