Clean JAVA Solution


  • 0
    A
    public List<String> letterCombinations(String digits) {
        List<String> list = new ArrayList<String>();
        if(digits == null || digits.isEmpty()) return list;
        Map<Character,String> map = new HashMap<Character,String>();
        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");
        letterCombinations(digits, 0, "", list, map);
        return list;   
    }
    
    void letterCombinations(String digits, int index, String str, List<String> list, Map<Character,String> map) {
        if(index == digits.length()) {
            list.add(str);
            return;
        }
        String values = map.get(digits.charAt(index));
        for(char ch: values.toCharArray()) {
            letterCombinations(digits, index+1, str+ch , list, map );
        }
    }

Log in to reply
 

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