DFS Java Solution


  • 0
    private String[] buttons = new String[] {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
    
    public List<String> letterCombinations(String digits) {
        List<String> result = new ArrayList<>();
        if (digits.length() == 0) {
            return result;
        }
        StringBuilder sb = new StringBuilder();
        DFS(digits, result, 0, sb);
        return result;
    }
    
    private void DFS(String digits, List<String> result, int level, StringBuilder sb) {
        if (level == digits.length()) {
            result.add(sb.toString());
            return;
        }
        for (int i = 0; i < buttons[Character.getNumericValue(digits.charAt(level))].length(); i++) {
            sb.append(buttons[Character.getNumericValue(digits.charAt(level))].charAt(i));
            DFS(digits, result, level + 1, sb);
            sb.deleteCharAt(sb.length() - 1);
        }
    }

Log in to reply
 

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