Reverse Words in a String III - Problem
Word Reversal Challenge: Given a string s containing multiple words separated by spaces, your task is to reverse the characters within each individual word while keeping the words in their original positions and preserving all whitespace.

Think of it as flipping each word like a pancake - the word order stays the same, but each word gets flipped individually. For example, "Hello World" becomes "olleH dlroW".

Goal: Transform each word by reversing its character order
Input: A string with words separated by single spaces
Output: The same string with each word's characters reversed

Input & Output

example_1.py — Basic word reversal
$ Input: s = "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
💡 Note: Each word is reversed individually: 'Let's' becomes 's'teL', 'take' becomes 'ekat', 'LeetCode' becomes 'edoCteeL', and 'contest' becomes 'tsetnoc'. The spaces between words are preserved.
example_2.py — Single word
$ Input: s = "God"
Output: "doG"
💡 Note: With only one word, we simply reverse its characters: 'G' and 'd' swap positions while 'o' stays in the middle, resulting in 'doG'.
example_3.py — Mixed length words
$ Input: s = "I love programming"
Output: "I evol gnimmargor"
💡 Note: Single character 'I' remains unchanged, 'love' becomes 'evol', and 'programming' becomes 'gnimmargor'. Each word boundary is preserved by the spaces.

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

Visualization

Tap to expand
Reverse Words in a String III INPUT Input String s: Let's take LeetCode con.. Character View: L e t ' s 0 1 2 3 4 Words Identified: ["Let's", "take", "LeetCode", "contest"] Full Input String: "Let's take LeetCode contest" 4 words, spaces preserved ALGORITHM STEPS 1 Split by Spaces Identify word boundaries 2 Reverse Each Word Two-pointer technique 3 Keep Word Order Original positions stay 4 Join with Spaces Reconstruct string Word Reversal Demo: Before: L e t ' s After: s ' t e L = "s'teL" FINAL RESULT Each word reversed: s'teL ekat edoCteeL tset.. Transformation: Let's --> s'teL take --> ekat LeetCode --> edoCteeL contest --> tsetnoc OUTPUT: "s'teL ekat edoCteeL tsetnoc" OK - Words Reversed! Key Insight: The optimal O(n) solution uses two pointers to reverse each word in-place. Start and end pointers swap characters while moving toward each other. This avoids extra space for storing words. Time: O(n) | Space: O(1) for in-place, O(n) if creating new string. Spaces remain untouched! TutorialsPoint - Reverse Words in a String III | Optimal Two-Pointer Approach Time O(n) | Space O(1)
Asked in
Amazon 15 Microsoft 12 Google 8 Apple 6
85.0K Views
Medium Frequency
~15 min Avg. Time
3.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen