Java Strightforward solution


  • 0
    Z
    public class Solution {
        
        static ArrayList<String> lets[];
        static{
            lets = new ArrayList[10];
            for(int i = 0;i<10;i++) lets[i] = new ArrayList<>();
            lets[0].add(" ");
            char ch = 'a';
            for(int i = 2;i<7;i++){
                for(int j = 0;j<3;j++){
                    lets[i].add(""+ch++);
                }
            }
            for(int i = 0;i<4;i++){
                lets[7].add(""+ch++);
            }
            for(int i = 0;i<3;i++) {
                lets[8].add(""+ch++);
            }
            for(int i = 0;i<4;i++){
                lets[9].add(""+ch++);
            }
            
        }
        
        public List<String> letterCombinations(String digits) {
            List<String> res=  new ArrayList<>();
            if(digits == null || digits.isEmpty()) return res;
            res.addAll(Solution.lets[digits.charAt(0)-'0']);
            for(int i = 1;i<digits.length();i++){
                int curSize = res.size();
                for(int j = 0;j<curSize;j++){
                    String str = res.get(0);
                    res.remove(0);
                    for(int jj = 0;jj<Solution.lets[digits.charAt(i)-'0'].size();jj++){
                        String s = Solution.lets[digits.charAt(i)-'0'].get(jj);
                        res.add(str+s);
                    }
                }
            }
            return res;
        }
    }
    

Log in to reply
 

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