Community Problem
Reverse Words in a String III
Difficulty: Easy
Reverse the characters of every word in a sentence while keeping the word order intact.
Reverse Words in a String III
Reverse the characters of every word in a sentence while keeping the word order intact.
By @meinakamura
March 5, 2026
·
Updated May 18, 2026
765 views
7
4.3 (11)
Asked this on a phone screen last quarter and the candidate immediately reached for s.split(' ') plus a list comprehension. Works, but it costs O(n) extra space. The interesting variant is the in-place pass: walk the array, find each word's bounds, reverse that slice, repeat. Same O(n) time as the split approach, but O(1) extra space. Worth practicing because real interviewers love asking for the constant-space version after the easy answer.
Reverse Words in a String III
Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and the initial word order. Words are separated by single spaces, and the string contains no leading or trailing spaces.
Examples
Example 1:
- Input:
s = "Let's take LeetCode contest" - Output:
"s'teL ekat edoCteeL tsetnoc" - Explanation: Each word is reversed individually, the spaces between words stay in place.
Example 2:
- Input:
s = "Mr Ding" - Output:
"rM gniD" - Explanation: Two words reversed, the single space preserved.
Example 3:
- Input:
s = "a" - Output:
"a" - Explanation: Single character, single word; reversal of a one-character word is a no-op.
Example 4:
- Input:
s = "hello" - Output:
"olleh" - Explanation: One word, reversed.
Constraints
1 <= s.length <= 5 * 10^4scontains printable ASCII characters.sdoes not contain any leading or trailing spaces.- All words are separated by a single space.
Follow-up
Can you do it in O(1) extra space? (Easier in C++ where strings are mutable; in JS / Python you can still hit O(1) auxiliary space if you track word bounds without allocating intermediate arrays.)
Solution
Hints
Approach
Walk the character array once, locate each word's [start, end) window, then reverse just that slice in place. Because the input has single-space separators and no edge whitespace, word detection is trivial: a word starts where a non-space sits at index i after either the start of the array or a space, and it ends at the first space (or end of array) past i.
Algorithm
Per-word in-place reverse
1. i = 0
2. while i < n:
if chars[i] is space: i += 1, continue
j = i
while j < n and chars[j] is not space: j += 1
reverse chars[i..j-1] using two pointers
i = j
3. join the array and returnComplexity
| Metric | Cost |
|---|---|
| Time | O(n) |
| Space | O(n) |
Time is linear because every character is visited at most twice: once by the outer scan and once by the inner reverse. Space is O(n) for the mutable character array (JS / Python strings are immutable). The split(' ') + map + join alternative is also O(n) time but allocates an additional array of words on top of the character buffer.
Why it works
Reversing a single word's slice does not affect any character outside that slice. The inter-word spaces are at fixed indices and are never moved. So the global word order is preserved by construction, and each word's local order is reversed by the inner two-pointer swap. The two effects compose into the required output.
Pitfalls
- Trying to mutate the string directly. JS / Python strings are immutable; you must work on an array.
- Off-by-one on word end. After the inner scan,
jpoints at the space (or end of array), so the word's last character is atj - 1, notj. - Forgetting to advance
ipast the word. If you onlyi++after a reverse, you re-enter the same word at indexi + 1and re-reverse it back to original. Seti = jinstead. - Assuming multiple spaces. The constraints guarantee single spaces and no edge whitespace, so do not over-engineer for
" hello world".
Solution Code
