My java solution for reference


  • 1
    R

    It is a typical DFS solution.

    public class Solution {
    	Map<Character,Character[]> map; 
    	char[] array;
    	char[] cons;
    	List<String> result;
    	public Solution(){
    		map = new HashMap<Character,Character[]>();
    		map.put('2', new Character[]{'a','b','c'});
    		map.put('3', new Character[]{'d','e','f'});
    		map.put('4', new Character[]{'g','h','i'});
    		map.put('5', new Character[]{'j','k','l'});
    		map.put('6', new Character[]{'m','n','o'});
    		map.put('7', new Character[]{'p','q','r','s'});
    		map.put('8', new Character[]{'t','u','v'});
    		map.put('9', new Character[]{'w','x','y','z'});
    	}
        public List<String> letterCombinations(String digits) {
        	result= new LinkedList<String>();
        	if (digits==null||digits.length()==0){
        		result.add("");
        		return result;
        	}
        	array=digits.toCharArray();
        	
        	cons=new char[digits.length()];
        	for (Character ch:map.get(array[0]))	
        		letterCombinationsHelper(0,ch);
        	return result;        
        }
        private void letterCombinationsHelper(int i,char c){
        	cons[i++]=c;
        	if (i==array.length){
        		result.add(new String(cons));
        		return;
        	}
        	for (Character ch:map.get(array[i]))
        		letterCombinationsHelper(i,ch);    	
        }
    }

  • 1
    S
    public class Solution {
        Map<Integer,List<String>> map;
        public List<String> letterCombinations(String digits) {
            map = new HashMap<Integer,List<String>>();
            this.map.put(2,new ArrayList<String>(Arrays.asList("a","b","c")));
            this.map.put(3,new ArrayList<String>(Arrays.asList("d","e","f")));
            this.map.put(4,new ArrayList<String>(Arrays.asList("g","h","i")));
            this.map.put(5,new ArrayList<String>(Arrays.asList("j","k","l")));
            this.map.put(6,new ArrayList<String>(Arrays.asList("m","n","o")));
            this.map.put(7,new ArrayList<String>(Arrays.asList("p","q","r","s")));
            this.map.put(8,new ArrayList<String>(Arrays.asList("t","u","v")));
            this.map.put(9,new ArrayList<String>(Arrays.asList("w","x","y","z")));
            return comb(digits);
            
        }
        public List<String> comb(String digits) {
            List<String> result = new LinkedList<String>();
            if(digits.length() == 0) {
                result.add(new String(""));
            } else if(digits.length() == 1) {
                List<String> ret = new LinkedList<String>();
                for(String c:map.get(Integer.parseInt(digits)))
                    ret.add(c);
                return ret;
            } else {
                for(String s : comb(digits.substring(1))) {
                    for(String c:map.get(Integer.parseInt(digits.substring(0,1))))
                        result.add(c + s);
                }
            }
            return result;
        }
    }
    

  • 0
    B
    public class Solution {
        private final static Map<Character, char[]> digitToLettersMap = 
                new HashMap<>();
        static{
            digitToLettersMap.put('2', new char[]{'a', 'b', 'c'});
            digitToLettersMap.put('3', new char[]{'d', 'e', 'f'});
            digitToLettersMap.put('4', new char[]{'g', 'h', 'i'});
            digitToLettersMap.put('5', new char[]{'j', 'k', 'l'});
            digitToLettersMap.put('6', new char[]{'m', 'n', 'o'});
            digitToLettersMap.put('7', new char[]{'p', 'q', 'r', 's'});
            digitToLettersMap.put('8', new char[]{'t', 'u', 'v'});
            digitToLettersMap.put('9', new char[]{'w', 'x', 'y', 'z'});
        }        
        
        private List<String> letterCombinationsInt(String digits) {
            if(digits.isEmpty())
                return Arrays.asList("");
            List<String> resultList = new ArrayList<>();
            for(String str: letterCombinationsInt(digits.substring(1)))
                for(char c: digitToLettersMap.get(digits.charAt(0)))
                    resultList.add(c + str);
            return resultList;
        }
        
        public List<String> letterCombinations(String digits) {
            if(digits == null || digits.isEmpty())
                return Collections.emptyList();
            
            return letterCombinationsInt(digits);
        }
    }

Log in to reply
 

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