Concise Java solution


  • 0

    Clean and straight-forward solution.
    Time complexity O(n), space O(n)

    public class Solution {
        public String reverseWords(String s) {
            String[] words = s.split("\\s+");
            StringBuilder sb = new StringBuilder();
            for(String word : words)
            {
                sb.append((new StringBuilder(word)).reverse());
                sb.append(" ");
            }
            sb.setLength(sb.length()-1);
            return sb.toString();
        }
    }
    

Log in to reply
 

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