2ms C++ solution


  • 0
    A

    Using a hash map and iterate from the last letter of the digits string.

    class Solution {
        public:
            vector<string> letterCombinations(string digits) {
                map<char, string> table;
                table['2'] = "abc";
                table['3'] = "def";
                table['4'] = "ghi";
                table['5'] = "jkl";
                table['6'] = "mno";
                table['7'] = "pqrs";
                table['8'] = "tuv";
                table['9'] = "wxyz";
                
                vector<string> res;
                for(int i = digits.length() - 1; i >= 0; --i){
                    char temp = digits.at(i);
                    if('2' <= temp && '9' >= temp){
                        if(i == digits.length() - 1){
                            string inner = table[temp];
                            for(int j = 0; j < inner.length(); ++j){
                                res.push_back(string(1,inner[j]));
                            }
                        }else{
                            string inner = table[temp];
                            vector<string> current(res.begin(), res.end());
                            res.clear();
                            for(int j = 0; j < inner.length(); ++j){
                                for(string str: current){
                                    res.push_back(inner[j] + str);
                                }
                            }
                        }
                    }
                }
                
                return res;
            }
        };

Log in to reply
 

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