Java 2-liner


  • 0
    public String reverseWords(String s) {
        String[] revWords = Stream.of(s.split(" ")).map(w -> new StringBuilder(w).reverse().toString()).toArray(String[]::new);
        return String.join(" ", revWords);
    }  
    

  • 0
    S

    1-liner with the join collector:

    public String reverseWords(String s) {
            return Stream.of(s.split(" ")).map(w->new StringBuilder(w).reverse().toString()).collect(Collectors.joining(" "));
        }
    

Log in to reply
 

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