JAVA DP Solution


  • 0
    H

    DP Solution. Only need to save the result of the last valid digit's result. Performance is good.

        public List<String> letterCombinations(String digits) {
            
            List<String> res = new ArrayList<String>();
    
            for(int i=0;i<digits.length();i++){
                if(digits.charAt(i)-'2' < 0 || digits.charAt(i)-'9' > 0  )  {return res;    }
                else    {continue;}            
            }
    
            char[][] ref = {{'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'}};
            res.add("");
            
            for(int i=0;i<digits.length();i++){
                
            	int index;
                if(digits.charAt(i)-'2' >= 0 && digits.charAt(i)-'9' <= 0  ){
                     index = (int) digits.charAt(i)-'2';
                }else   {continue;}
         
                int len = res.size();
                for(int j=0;j<len;j++){
                    
                    String str = res.remove(0);
                    for(char ch: ref[index]){
                        res.add(str+ch);
                    }
                    
                }
            }
            
            if(res.size() == 1){
            	res.remove(0);
            }
            return res;
           
        }
    

Log in to reply
 

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