Simple solution


  • 0
    S

    class Solution {
    public:
    vector <string> res;
    string fun(char n)
    {
    //cout<<"n = "<<n<<"\n";
    if(n=='2')
    return "abc";
    else if(n=='3')
    return "def";
    else if(n=='4')
    return "ghi";
    else if(n=='5')
    return "jkl";
    else if(n=='6')
    return "mno";
    else if(n=='7')
    return "pqrs";
    else if(n=='8')
    return "tuv";
    else if(n=='9')
    return "wxyz";
    }
    void print(string s,int m,int n,char* t)
    {
    if(m==n)
    {
    //cout<<"t = "<<t;
    res.push_back(t);
    return;
    }
    else
    {
    for(int i=0;i<fun(s[m]).length();i++)
    {
    string h=fun(s[m]);
    t[m]=h[i];

    		print(s,m+1,n,t);
    		if(s[m]=='1'||s[m]=='0')
    			return;
    	}
    }		
    

    }
    vector<string> letterCombinations(string s) {
    if(s.length()==0)
    return res;
    char *t=new char[s.length()+1];
    t[s.length()]='\0';
    print(s,0,s.length(),t);
    return res;
    }
    };


Log in to reply
 

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