Java OOP code 2ms


  • 0
    A
    public class Solution {
        public List<String> letterCombinations(String digits) {
            if (digits == null || digits.length() == 0 || digits.contains("0") || digits.contains("1")) {
                return Collections.emptyList();
            }
    
            String[] keys = new String[] {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
            Tracer tracer = new Tracer(keys, digits.toCharArray());
            tracer.trace();
            return tracer.getResult();
        }
        
        public class Tracer {
            private List<String> res;
            private String[] keys;
            private char[] digits;
            
            public Tracer(String[] keys, char[] digits) {
                this.keys = keys;
                this.digits = digits;
                this.res = new LinkedList<>();
            }
    
            public void trace() {
                innerTrace(new StringBuilder());
            }
            
            private void innerTrace(StringBuilder sb) {
                if (sb.length() == digits.length) {
                    res.add(sb.toString());
                    return;
                }
                
                char[] arr = keys[digits[sb.length()] - '0'].toCharArray();
                for (char c : arr) {
                    sb.append(c);
                    innerTrace(sb);
                    sb.deleteCharAt(sb.length() - 1);
                }
            }
            
            public List<String> getResult() {
                return res;
            }
        }
    }
    

Log in to reply
 

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