Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

&nbsp;
Example 1:
Input: s = "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
Example 2:
Input: s = "God Ding"
Output: "doG gniD"

&nbsp;
Constraints:


	1 &lt;= s.length &lt;= 5 * 104
	s contains printable ASCII characters.
	s does not contain any leading or trailing spaces.
	There is at least one word in s.
	All the words in s are separated by a single space.


class Solution:
    def reverseWords(self, s: str) -> str:
        s = s + ' '
        l = len(s)
        t = ''
        w = ''
        for i in range(l):
            if s[i]!=' ':
                t = s[i] + t  # t stores the word in reverse order
            else:
				# w stores the reversed word in the same order
                w = w + t + ' ' # could have used .join() function and not write .strip()
                t = "" # value of t is null so that it won't affect upcoming words
        return w.strip() # removes extra whitespace
class Solution {
    public String reverseWords(String s) {
        if(s == null || s.trim().equals("")){
            return null;
        }
        String [] words = s.split(" ");
        StringBuilder resultBuilder = new StringBuilder();
        for(String word: words){
            for(int i = word.length() - 1; i>=0; i --){
                resultBuilder.append(word.charAt(i));
            }
            resultBuilder.append(" ");
        }
        return resultBuilder.toString().trim();
    }
}
Time: O(n+n)  Space: O(1)

class Solution {
public:
    string reverseWords(string s) {
        int i,j;
        for( i=0,j=0;i<size(s);i++){
            if(s[i]==' '){
                reverse(begin(s)+j,begin(s)+i);
                j=i+1;
            }
        }
        reverse(begin(s)+j,end(s));
        return s;
    }
};
var reverseWords = function(s) {
    // make array of the words from s
    let words = s.split(" ");
    for (let i in words) {
	    // replace words[i] with words[i] but reversed
        words.splice(i, 1, words[i].split("").reverse().join(""))
    } return words.join(" ");
};
Reverse Words in a String III