Watch 10 video solutions for Reverse Words in a String III, a easy level problem involving Two Pointers, String. This walkthrough by Knowledge Center has 27,900 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |