Java Solution


  • 0
    E
        public List<String> letterCombinations(String digits) {
            TreeSet<String> result = new TreeSet<String>();
            getCombinations(digits, "", result);
            result.remove("");
            return new ArrayList<String>(result);
        }
        
        public void getCombinations(String digits, String curr, TreeSet<String> result) {
            if (digits.equals(""))
                result.add(curr);
            else {
                if (digits.charAt(0) == '2') {
                    getCombinations(digits.substring(1), curr + "a", result);
                    getCombinations(digits.substring(1), curr + "b", result);
                    getCombinations(digits.substring(1), curr + "c", result);
                } else if (digits.charAt(0) == '3') {
                    getCombinations(digits.substring(1), curr + "d", result);
                    getCombinations(digits.substring(1), curr + "e", result);
                    getCombinations(digits.substring(1), curr + "f", result);
                } else if (digits.charAt(0) == '4') {
                    getCombinations(digits.substring(1), curr + "g", result);
                    getCombinations(digits.substring(1), curr + "h", result);
                    getCombinations(digits.substring(1), curr + "i", result);
                } else if (digits.charAt(0) == '5') {
                    getCombinations(digits.substring(1), curr + "j", result);
                    getCombinations(digits.substring(1), curr + "k", result);
                    getCombinations(digits.substring(1), curr + "l", result);
                } else if (digits.charAt(0) == '6') {
                    getCombinations(digits.substring(1), curr + "m", result);
                    getCombinations(digits.substring(1), curr + "n", result);
                    getCombinations(digits.substring(1), curr + "o", result);
                } else if (digits.charAt(0) == '7') {
                    getCombinations(digits.substring(1), curr + "p", result);
                    getCombinations(digits.substring(1), curr + "q", result);
                    getCombinations(digits.substring(1), curr + "r", result);
                    getCombinations(digits.substring(1), curr + "s", result);
                } else if (digits.charAt(0) == '8') {
                    getCombinations(digits.substring(1), curr + "t", result);
                    getCombinations(digits.substring(1), curr + "u", result);
                    getCombinations(digits.substring(1), curr + "v", result);
                } else if (digits.charAt(0) == '9') {
                    getCombinations(digits.substring(1), curr + "w", result);
                    getCombinations(digits.substring(1), curr + "x", result);
                    getCombinations(digits.substring(1), curr + "y", result);
                    getCombinations(digits.substring(1), curr + "z", result);
                }
            }
        }
    }```

Log in to reply
 

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