Java Beat 82% Backtracking Recurssive solution


  • 0
    B
    public class Solution {
        List<String> res = new ArrayList<String>();
        public List<String> letterCombinations(String digits) {
            char[] inputChar = digits.toCharArray();
            List<Character> input = new ArrayList<Character>();
            for(char ch : inputChar) {
                if(lookUp(ch) != null) {
                    input.add(ch);
                }            
            }
            if(input.size() == 0) return res;
            dfs(input, "", 0);
            return res;
        }
        private void dfs(List<Character> input, String path, int inputIdx) {
            if(path.length() == input.size()) {
                res.add(path);
                return;
            }
            char[] chArr = lookUp(input.get(inputIdx));
            for(int i = 0; i < chArr.length; i++) { 
                dfs(input, path + chArr[i], inputIdx + 1);
            }
        }
        private char[] lookUp(char ch) {
            switch(ch) {
                case '2': return new char[] {'a', 'b', 'c'};            
                case '3': return new char[] {'d', 'e', 'f'};
                case '4': return new char[] {'g', 'h', 'i'};
                case '5': return new char[] {'j', 'k', 'l'};
                case '6': return new char[] {'m', 'n', 'o'};
                case '7': return new char[] {'p', 'q', 'r', 's'};
                case '8': return new char[] {'t', 'u', 'v'};
                case '9': return new char[] {'w', 'x', 'y', 'z'};
                default: return null;
            }
    
        }
    }
    

Log in to reply
 

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