Simple Java - Regex + SB + trim()


  • 0
        public String reverseWords(String s) {
            String[] words = s.split("\\s+");
            if (words.length == 0) return ""; 
            
            StringBuilder sb = new StringBuilder(); 
            for (String word : words) 
                sb.insert(0, word + " "); 
                
            return sb.toString().trim(); 
        }

Log in to reply
 

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