Simple Java Solution


  • 0
    J
    public List<String> letterCombinations(String digits) {
         if(digits==null||digits.length()==0)return Collections.emptyList();
         
         String[] dic={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
         List<String> res=new ArrayList<String>();
         res.add("");
        for(int i=0;i<digits.length();i++){
            int cur=Integer.parseInt(digits.substring(i,i+1));
            if(cur==1||cur==0)continue;
            List<String> temp=new ArrayList<String>();
            for(String s:res){
                for(int j=0;j<dic[cur].length();j++){
                    temp.add(s+dic[cur].charAt(j));
                }
            }
            res=temp;
        }
        return res;
    }

Log in to reply
 

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