Java solution using Stream


  • 0
    C
    public List<String> letterCombinations(String digits) {
            String[] mapping = new String[] {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
            LinkedList<String> result = new LinkedList<>();
            
            if(digits.length() == 0 || digits == null){         
                return result;
            }
       
            result.add("");
            for(int i = 0; i < digits.length(); i++){                  
                String digitLetters = mapping[digits.charAt(i) - '0'];  
            
                LinkedList<String> temp = result                        
                                        .stream()
                                        .flatMap(str1 -> digitLetters.chars().mapToObj(j -> (char)j).map(str2 -> str1 + str2))
                                        .collect(Collectors.toCollection(LinkedList::new));
                result = temp;
            }
            return result;
        }
    

Log in to reply
 

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