Java easy recursive solution.


  • 0
    R
        public List<String> letterCombinations(String digits) {
            List<String> sol = new ArrayList<String>();
            if(digits == null || digits.equals("") || digits.contains("0") || digits.contains("1")){
                return sol;
            }
            helper("",0,sol,digits);
            return sol;
        }
        public void helper(String curr, int pos, List<String> sol, String digits){
            if(pos==digits.length()){
                sol.add(curr);
                return;
            }
            int digit = Integer.parseInt(""+digits.charAt(pos));
            int map = digit-2;
            map = (digit<=7 ? map*3 : map*3 +1);
            int max = (digit==9 || digit ==7 ? map+4 : map+3);
            for(int i = map; i<max; i++){
                char c = (char) (((int) 'a') + i);
                helper(curr + c, pos+1, sol, digits);
            }
        }
    } 
    

Log in to reply
 

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