Java 6 lines BFS solution


  • 0
        final String[] map = new String[] { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
    
        public List<String> letterCombinations(String digits) {
            LinkedList<String> q = new LinkedList<>(Arrays.asList(""));
            while (q.peek().length() != digits.length()) {
                String s = q.poll();
                for (char child : map[digits.charAt(s.length()) - '0'].toCharArray()) q.offer(s + child);
            }
            return q.size() == 1 ? Collections.emptyList() : q;
        }
    

Log in to reply
 

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