Letter Combinations of a phone number c++ program


  • 0
    P
    class Solution {
    public:
        vector<vector<char> > letters = {{}, {}, {'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<char> getletterlist(char c)
        {
            return letters[c-'0'];
        }
        
        void letterCombinations(string digit, int index, string &s, vector<string> &v)
        {
            if(digit[index] == '\0')
            {
                if(!s.empty())
                    v.push_back(s);
                return;
            }
            
            vector<char> ch = getletterlist(digit[index]);
            for(int i = 0; i < ch.size(); i++)
            {
                s.push_back(ch[i]);
                letterCombinations(digit, index+1, s, v);
                s.pop_back();
            }
        }
        
        vector<string> letterCombinations(string digits) {
           vector<string> v;
            string s="";
            letterCombinations(digits, 0, s, v);
            return v;
        }
    };
    

Log in to reply
 

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