Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example 1:
Input: s = "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"
Example 2:
Input: s = "Mr Ding" Output: "rM gniD"
Constraints:
1 <= s.length <= 5 * 104s contains printable ASCII characters.s does not contain any leading or trailing spaces.s.s are separated by a single space.Problem Overview: You are given a sentence where words are separated by single spaces. The task is simple: reverse the characters of every word while keeping the original word order and spacing unchanged. For example, "Let's code" becomes "s'teL edoc".
Approach 1: Split and Reverse Each Word (O(n) time, O(n) space)
The most direct solution splits the string into words using the space character. Once you have the list of words, iterate through each one and reverse it individually using built‑in string slicing or a manual character reversal. After reversing all words, join them back together with spaces. The algorithm processes each character once during splitting and once during reversing, giving O(n) time complexity. Because the split operation creates a list of words and the join builds a new string, the extra space usage is O(n).
This approach is readable and easy to implement in languages like Python, JavaScript, or Java. In interviews, it demonstrates that you recognized the problem structure quickly. The tradeoff is the additional memory from storing intermediate words.
Approach 2: Two Pointers Inline Reversal (O(n) time, O(1) extra space)
A more efficient approach scans the string with the two pointers technique. Convert the string into a mutable character array if the language requires it. Use one pointer to mark the start of a word and iterate with another pointer until you hit a space or the end of the string. Once a word boundary is detected, reverse the characters between the start and end indices in place.
The key insight: each word can be reversed independently without affecting others. This allows an in-place swap using two pointers moving toward each other. Every character participates in at most one reversal, so the runtime remains O(n). Since the reversal happens inside the same character buffer, the additional memory usage is O(1) beyond the output string.
This technique is a classic application of the string manipulation pattern used in many interview questions. It avoids unnecessary allocations and scales well for large inputs.
Recommended for interviews: The two‑pointers inline reversal approach is typically what interviewers expect. The split‑and‑reverse method shows you understand the problem quickly, but the in‑place pointer solution demonstrates stronger algorithmic thinking and memory awareness. Both run in O(n) time, but the two‑pointer approach achieves optimal space usage and reflects common string manipulation patterns tested in technical interviews.
This approach involves splitting the given string into words, reversing each word individually, and then joining them back together.
This C solution involves iterating over the string and reversing each word individually. We keep track of the start of each word and reverse the word in place when we encounter a space.
Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1), as only a constant amount of extra space is used.
This involves using a two-pointer technique to reverse the characters within each word in place, thus preserving the overall space complexity.
In this C solution, a two-pointer technique is applied to reverse individual words within the string in place, using a helper function.
Time Complexity: O(n)
Space Complexity: O(1)
We can split the string s into an array of words words by spaces, then reverse each word and concatenate them back into a string.
The time complexity is O(n), and the space complexity is O(n). Here, n is the length of the string s.
Python
Java
C++
Go
TypeScript
Rust
JavaScript
PHP
| Approach | Complexity |
|---|---|
| Split and Reverse Each Word | Time Complexity: O(n), where n is the length of the string. |
| Two Pointers Inline Reversal | Time Complexity: O(n) |
| Simulation | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Split and Reverse Each Word | O(n) | O(n) | Best for quick implementation and readability when extra memory is acceptable |
| Two Pointers Inline Reversal | O(n) | O(1) | Preferred for interviews or memory‑constrained environments where in‑place string manipulation is required |
Reverse Words in a String iii | LeetCode 557 | C++ • Knowledge Center • 27,900 views views
Watch 9 more video solutions →Practice Reverse Words in a String III with our built-in code editor and test cases.
Practice on FleetCode