Skip to main content

Reverse Words in a String III - Solution & Explanation

EasyTwo PointersString13 min readAsked at: Amazon, Microsoft, Meta +4
Practice this problem

Problem Statement

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 * 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.

Approach Overview

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 1: Split and Reverse Each Word

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 2: Two Pointers Inline Reversal

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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

Try this approach in the editor →

Approach 3: Simulation

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.

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

PHP

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Split and Reverse Each Word

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.

Two Pointers Inline Reversal

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

Simulation

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Split and Reverse Each WordO(n)O(n)Best for quick implementation and readability when extra memory is acceptable
Two Pointers Inline ReversalO(n)O(1)Preferred for interviews or memory‑constrained environments where in‑place string manipulation is required

Video Solution

Reverse Words in a String iii | LeetCode 557 | C++Knowledge Center27,959 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Reverse Words in a String III easy or hard?
Reverse Words in a String III is classified as an Easy problem on LeetCode with a high acceptance rate. It mainly tests basic string manipulation and familiarity with the two-pointer pattern.
How to solve Reverse Words in a String III in O(n)?
Traverse the string and detect word boundaries using spaces. For each word, apply a two-pointer reversal between its start and end indices. Because each character is processed only once, the algorithm completes in O(n) time.
Reverse Words in a String III Python or Java solution
In Python, a common solution uses split(), reverse slicing for each word, and join() to rebuild the sentence. In Java, you can convert the string to a char array and reverse each word using two pointers before constructing the final string.
What is the best approach for Reverse Words in a String III?
The two pointers inline reversal approach is considered the best solution. It scans the string once, identifies word boundaries, and reverses characters in place. This achieves O(n) time complexity with O(1) extra space, which is optimal for this problem.
What data structure is used in Reverse Words in a String III?
The problem primarily uses strings and sometimes a character array for in-place modification. The algorithm relies on the two-pointer technique to swap characters within each word efficiently.
What is the time complexity of Reverse Words in a String III?
The optimal time complexity is O(n), where n is the length of the input string. Each character is visited at most a constant number of times during scanning and reversal. Both the split-based and two-pointer solutions run in linear time.
Is Reverse Words in a String III asked at Google, Amazon, or Meta?
String manipulation problems like this frequently appear in coding interviews at companies such as Amazon, Google, and Meta. While this exact question may vary, the underlying patterns—two pointers and in-place string reversal—are common interview topics.

Ready to solve this problem?

Practice Reverse Words in a String III with our built-in code editor and test cases.

Practice on FleetCode