Posted on

Description

Submission

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        string table[] = {"abc", "def", "ghi", "jkl", 
                      "mno", "pqrs", "tuv", "wxyz"};
        vector<string> t1;
        vector<string>& res = t1;
        for(auto digit: digits) {
            string s = table[digit - '2'];
            if(res.empty()) {
                for(char c: s) {
                    res.push_back(string(1, c));
                }
            } else {
                vector<string> tmp;
                for(char c: s) {
                    for(auto p: res) {
                        tmp.push_back(p + string(1, c));
                    }
                }
                res = tmp;
            }
        }
        return res;
    }

};

References

It’s tricky to convert a char to a single character string

One way is to use the string constructor, taking two parameters, the first is the repentance of the character, second being the char.

Leave a Reply

Your email address will not be published. Required fields are marked *