Reverse Words in String III

EasyStringSorting

Description

Given a string s, reverse the order of characters in each word within a sentence while preserving whitespace and initial word order. A word is defined as a sequence of non-space characters. Return the result as a string.

Examples

Input:s = "Let's take LeetCode contest"
Output:"s'teL ekat edoCteeL tsetnoc"
Explanation:

Each word is reversed individually while preserving spaces: 'Let's' → 's'teL', 'take' → 'ekat', 'LeetCode' → 'edoCteeL', 'contest' → 'tsetnoc'.

Input:s = "Hello World"
Output:"olleH dlroW"
Explanation:

Each word is reversed individually: 'Hello' becomes 'olleH' and 'World' becomes 'dlroW', while the space between them is preserved.

Input:s = "a"
Output:"a"
Explanation:

Single character strings remain unchanged when reversed, demonstrating the edge case of minimal input length.

Constraints

  • 1 ≤ s.length ≤ 5 × 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!