Easy to understand. C++ implementation. 0ms


  • 0
    N
    class Solution {
    public:
        vector<string>vv;
        map<char,string>mp;
        void rec(string digits,int i,string ans)
        {
            if(i==digits.length())
            {
                vv.push_back(ans);
                return;
            }
            string s=mp[digits[i]];
            for(int j=0;j<s.length();j++)
            {
                rec(digits,i+1,ans+s[j]);
            }
        }
        vector<string> letterCombinations(string digits) {
            if(digits.length()==0)return vv;
            int i,j;
            mp['2']="abc";mp['3']="def";mp['4']="ghi";mp['5']="jkl";mp['6']="mno";mp['7']="pqrs";mp['8']="tuv";mp['9']="wxyz";
            string k;
            for(i=0;i<digits.length();i++)
            {
                if(digits[i]=='1'||digits[i]=='0'||digits[i]=='*'||digits[i]=='#')return vv;
            }
            rec(digits,0,"");
            return vv;
        }
    };

Log in to reply
 

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