package leetcode;

/**
 * https://leetcode.com/problems/reverse-words-in-a-string-iii/
 */
public class Problem557 {
    public String reverseWords(String s) {
        StringBuilder sb = new StringBuilder();
        String[] split = s.split(" ");
        for (int i = 0; i < split.length; i++) {
            if (i == 0) {
                sb.append(reverse(split[i].toCharArray()));
            } else {
                sb.append(" " + reverse(split[i].toCharArray()));
            }
        }
        return sb.toString();
    }

    private static String reverse(char[] chars) {
        for (int i = 0, j = chars.length - 1; i < j; i++, j--) {
            char tmp = chars[i];
            chars[i] = chars[j];
            chars[j] = tmp;
        }
        return new String(chars);
    }

    public static void main(String[] args) {
        Problem557 prob = new Problem557();
        System.out.println(prob.reverseWords("Let's take LeetCode contest"));
        System.out.println(prob.reverseWords("Hello"));
        System.out.println(prob.reverseWords(""));
    }
}

@@ -785,288 +785,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem557 prob = new Problem557();%0A        System.out.println(prob.reverseWords(%22Let's take LeetCode contest%22));%0A        System.out.println(prob.reverseWords(%22Hello%22));%0A        System.out.println(prob.reverseWords(%22%22));%0A    %7D
 %0A%7D%0A

Update problem 557