My Java Solution


  • 0
    H
    LinkedList<String> results = new LinkedList<>();
    if(digits.matches("[^2-9]"))
            	throw new IllegalArgumentException("Illegal input!");
            Map<Character,List<Character>> map = new HashMap<>();
            map.put('2', Arrays.asList('a','b','c'));
            map.put('3', Arrays.asList('d','e','f'));
            map.put('4', Arrays.asList('g','h','i'));
            map.put('5', Arrays.asList('j','k','l'));
            map.put('6', Arrays.asList('m','n','o'));
            map.put('7', Arrays.asList('p','q','r','s'));
            map.put('8', Arrays.asList('t','u','v'));
            map.put('9', Arrays.asList('w','x','y','z'));
            int len = digits.length();
            results.add("");
            for(int i =0;i<len;i++){
            	char digit = digits.charAt(i);
            	while(results.peek().length()==i){
            		String t = results.remove();
            		for(char s:map.get(digit)){
            			results.add(t+s);
            		}
            	}
            }
            return results;
    

Log in to reply
 

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