My Java Solution Based on HashMap


  • 0
    E

    public List<String> letterCombinations(String digits) {

    String digitletter[] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        HashMap<Character,String> map =new HashMap<Character,String>();
        map.put('0',"");
        map.put('1',"");
        map.put('2',"abc");
        map.put('3',"def");        
        map.put('4',"ghi");
        map.put('5',"jkl");   
        map.put('6',"mno");
        map.put('7',"pqrs");   
        map.put('8',"tuv");
        map.put('9',"wxyz");   
        List<String> result = new ArrayList<String>();
    
        if (digits.length()==0) return result;
            
            result.add("");
            for (int i=0; i<digits.length(); i++) 
                result = combine(map.get(digits.charAt(i)),result);
            
            
            return result;
        }
        
        public static List<String> combine(String substring, List<String> l) {
            List<String> result = new ArrayList<String>();
            
            for (int i=0; i<substring.length(); i++) 
                for (String x : l) 
                    result.add(x+substring.charAt(i));
    
            return result;
        }

Log in to reply
 

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