JAVA SIMPLE SOLUTION 2MS


  • 0
    S

    public class Solution {
    public List<String> letterCombinations(String digits) {
    ArrayList<String> w=new ArrayList<String>();
    if(digits.length()==0){return w;}
    int i=digits.length();
    letter(digits,w,"",i,0);
    return w;
    }
    public void letter(String d,ArrayList<String> m,String n,int a,int b){
    if(a>0){char q=d.charAt(b);b=b+1;
    if(q=='0'||q=='1'){letter(d,m,n,a-1,b);}
    if(q=='2'){letter(d,m,n+"a",a-1,b);letter(d,m,n+"b",a-1,b);letter(d,m,n+"c",a-1,b);}
    if(q=='3'){letter(d,m,n+"d",a-1,b);letter(d,m,n+"e",a-1,b);letter(d,m,n+"f",a-1,b);}
    if(q=='4'){letter(d,m,n+"g",a-1,b);letter(d,m,n+"h",a-1,b);letter(d,m,n+"i",a-1,b);}
    if(q=='5'){letter(d,m,n+"j",a-1,b);letter(d,m,n+"k",a-1,b);letter(d,m,n+"l",a-1,b);}
    if(q=='6'){letter(d,m,n+"m",a-1,b);letter(d,m,n+"n",a-1,b);letter(d,m,n+"o",a-1,b);}
    if(q=='7'){letter(d,m,n+"p",a-1,b);letter(d,m,n+"q",a-1,b);letter(d,m,n+"r",a-1,b);letter(d,m,n+"s",a-1,b);}
    if(q=='8'){letter(d,m,n+"t",a-1,b);letter(d,m,n+"u",a-1,b);letter(d,m,n+"v",a-1,b);}
    if(q=='9'){letter(d,m,n+"w",a-1,b);letter(d,m,n+"x",a-1,b);letter(d,m,n+"y",a-1,b);letter(d,m,n+"z",a-1,b);}
    }
    else{m.add(n);}
    }
    }


Log in to reply
 

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