My Recursive Java Solution


  • 0
    B

    Hi My Recursive Java Solution

    ``

    public List<String> letterCombinations(String digits) {
        String[] Phone = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        if(digits.length() == 0)
            return new LinkedList<String>();
            
        int x = digits.charAt(digits.length()-1) - '0';
        List<String> list = letterCombinations(new String(digits.toCharArray(), 0, digits.length()-1));
        while(list.isEmpty() || list.get(0).length() == digits.length() - 1) {
            String before = list.isEmpty() ? "" : list.remove(0);
            for (char letter : Phone[x].toCharArray()) {
                list.add(before + letter);
            }
        }
        return list;
    }
    

    }

    ``


Log in to reply
 

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