Concise Java Solution Using Standard Functions


  • 0
    N
        public String reverseWords(String s) {
            StringBuilder sb = new StringBuilder();
            for(String t: s.split(" ")) {
                if (!t.isEmpty()) sb.append(new StringBuilder(t.trim()).reverse() + " ");
            }
            return sb.reverse().toString().trim();
        }

Log in to reply
 

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