1ms JAVA simple one recursive function


  • -2
    W
    public List<String> letterCombinations(String digits) {
        if(digits == null || digits.length() == 0)
            return new ArrayList<String>();
        String[] map = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        List<String> result = letterCombinations(digits.substring(1));
        List<String> results = new ArrayList<String>();
        for(char c : map[digits.charAt(0)-'0'].toCharArray()){
            if(result.size() == 0)
                results.add(c + "");
            else{
               for(String r : result)
                   results.add(c + r);
            }
        }
        return results;
    }

Log in to reply
 

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