C++ solution with deque


  • 0
        vector<string> letterCombinations(string digits) {
            if (digits.empty()) return {};
            
            vector<string> letters = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
            deque<string> q = {""};
            int len = 0;
            for (char d : digits) {
                while (q.front().size() == len) {
                    auto s = q.front(); q.pop_front();
                    for (char c : letters[d-'0']) q.push_back(s + c);
                }
                len++;
            }        
            return vector<string>(q.begin(), q.end());
        }
    

Log in to reply
 

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