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

public class Solution {
    /**
     * @param s : A string
     * @return : A string
     */
    public String reverseWords(String s) {
        // write your code
        if(s == null || s.length() == 0) return s;
        String[] arr = s.split(" ");
        //split the original string to str array, use space as separator, there will be empty strings in the array
        StringBuilder str = new StringBuilder();
        for(int i = arr.length-1; i >= 0; i--){
            if(!arr[i].equals("")){
            //!!!important, use "" instead of " " to deal with empty strings, not space
                str.append(arr[i]).append(" ");
            }
        }
        return str.toString();
    }
}
class Solution(object):
    def reverseWords(self, s):
        """
        :type s: str
        :rtype: str
        """

        return " ".join(reversed([word for word in s.strip().split(" ") if word]))