557. Reverse Words in a String III

·

1 min read

https://leetcode.com/problems/reverse-words-in-a-string-iii/

class Solution {
    public String reverseWords(String s) {
        String[] array=s.split(" ");
        StringBuilder result=new StringBuilder();
        for(int i=0;i<array.length;i++){
            result.append(new StringBuilder(array[i]).reverse().toString()+" ");
        }
        return result.toString().trim();
    } 
}

Notes:

  • Use String.split() function
  • Use StringBuilder to reverse the String
  • Use StringBuilder and append method is faster than use String