Java 3ms recurse solution


  • 0
    G
    public class Solution {
        
        private final String [] mapping = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        
        private void helper(String digits, int index, List<String> result, String next) {
            if (digits.length() == index) {
                result.add(next);
                return;
            }
            
            String s = mapping[digits.charAt(index) - '0'];
            for (char c : s.toCharArray()) {
                helper(digits, index+1, result, next + c);
            }
        }
        
        public List<String> letterCombinations(String digits) {
            List<String> result = new ArrayList<String>();
            if (digits.length() == 0) return result;
            helper(digits, 0, result, "");
            return result;
        }
    }
    

Log in to reply
 

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