Easy understanding and fast Java solution


  • 0
    Y
    public List<String> letterCombinations(String digits) {
        List<String> result = new LinkedList<>();
        String[] table = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        for(int i = 0; i < digits.length(); i++){
            result = combine(result, table[digits.charAt(i) - '0']);
        }
        return result;
    }
    
    private List<String> combine(List<String> input, String temp){
        List<String> result = new LinkedList<>();
        for(int i = 0; i < temp.length(); i++){
            if(input.isEmpty()){
                result.add(temp.substring(i, i + 1));
            }else{
                for(String s: input){
                    result.add(s + temp.substring(i, i + 1));
                }
            }
        }
        return result;
    }

Log in to reply
 

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