> For the complete documentation index, see [llms.txt](https://joshualbarb.gitbook.io/leetcode-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://joshualbarb.gitbook.io/leetcode-problems/strings/string-simulation/reverse-words-in-a-string-iii.md).

# Reverse Words in a String III

String Simulation

## Problem&#x20;

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.&#x20;

**Note:** In the string, each word is separated by single space and there will not be any extra space in the string.

{% hint style="info" %}
For example:&#x20;

```
Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
```

{% endhint %}

### Thought Process

* This question is similar to **Reverse Words in a String** except here we aren't reversing the words of the sentence, only the letters of each word

## Solution

```
class Solution:
    def reverseWords(self, s: str) -> str:
        s = list(s)
        j = 0
        i = 0
        
        while i < len(s):
            while i < len(s) and s[i] != ' ':
                i+=1
            
            s[j:i] = s[j:i][::-1]
         
            i+=1
            j = i
        return("".join(s))
```

## Time Complexity

* **Time:** O(n)
* **Space:** O(1)
