## Title:

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

Given a string s, reverse the character order of each word in the sentence while still retaining the spaces and the original word order.

Example 1:

Input: s = "Let's take LeetCode contest"

Output: "s'teL ekat edoCteeL tsetnoc"

Example 1:

Enter: "Let's take LeetCode contest"

Output: "s'teL ekat edoCteeL tsetnoc"

Example 2:

Input: s = "God Ding"

Output: "doG gniD"

Example 2:

Input: "God Ding"

Output: "doG gniD"

Constraints:

- 1 <= s.length <= 5 * 1 0 4 10^4 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.

Tips:

- 1 <= s.length <= 5 * 1 0 4 10^4 104
- s contains printable ASCII characters.
- s does not contain any leading or trailing spaces.
- At least one word in s.
- All words in "s" are separated by a space.

## Problem solving ideas:

### Method 1: use additional space

Open up a new string. Then traverse the original string from beginning to end until a space is found. At this time, a word is found and the start and end positions of the word can be obtained. Then, according to the start and end position of the word, you can put the word in reverse order into the new string. This loop is repeated until the original string is traversed, and the flipped result can be obtained

C + + code

class Solution { public: string reverseWords(string s) { string res; int n = s.size(); int i = 0; while (i < n) { int start = i; while (i < n && s[i] != ' ') { i++; } for (int p = start; p < i; p++) { res.push_back(s[start + i - 1 - p]); } while (i < n && s[i] == ' ') { i++; res.push_back(' '); } } return res; } };

Java code

class Solution { public String reverseWords(String s) { StringBuffer res = new StringBuffer(); int n = s.length(); int i = 0; while (i < n) { int start = i; while (i < n && s.charAt(i) != ' ') { i++; } for (int p = start; p < i; p++) { res.append(s.charAt(start + i - 1 - p)); } while (i < n && s.charAt(i) == ' ') { i++; res.append(' '); } } return res.toString(); } }

Complexity analysis

- Time complexity: O(n), where n is the length of the string.
- Spatial complexity: O(n), where n is the length of the string.

### Method 2: in situ solution

This problem is also an upgraded version of method 1 to avoid additional space overhead. When we find a word, we exchange the first character and the penultimate character of the string, and then exchange the second character and the penultimate character... So repeatedly, we can flip the word in the original space.

be careful:

In situ solution is not applicable in some languages (such as Java, JavaScript) because String type is an immutable type in these languages.

C + + code

class Solution { public: string reverseWords(string s) { int ｎ= s.size(); int i = 0; while (i < ｎ) { int start = i; while (i < ｎ && s[i] != ' ') { i++; } int left = start, right = i - 1; while (left < right) { swap(s[left], s[right]); left++; right--; } while (i < ｎ && s[i] == ' ') { i++; } } return s; } };

Complexity analysis:

- Time complexity: O(N). Each character in the string is either exchanged to the corresponding position within the time of O(1), or remains fixed because it is a space.
- Space complexity: O(1), because there is no need to open up additional arrays.

### Python

Method: divide the string into word lists, and then invert each word into slices

class Solution(object): def reverseWords(self, s): return " ".join(word[::-1] for word in s.split(" "))

Complexity analysis:

- Time complexity: O(n), where n is the length of the string.
- Space complexity: O(1)