Given an input string, reverse the string word by word.

Example:
Given s = “the sky is blue”,
return “blue is sky the”.

Trick: trim before split.

1
2
3
4
5
6
7
8
9
    public String reverseWords(String s) {
        if( s == null || s=="" || s.length() == 0)
            return s;
        String[] strs = s.trim().split("\\s+");
        List<String> list = Arrays.asList(strs);
        Collections.reverse(list);
        strs = (String[]) list.toArray();
        return String.join(" ",strs);
    }