Reverse Words in a String III

Easy Topics Company Tags

Reverse the characters of every word in s. Keep the words in their original order and preserve the spaces separating them. Punctuation, digits, and other printable ASCII characters are part of their words.

Example 1:

Input: s = "a"

Output: "a"

Explanation: Reversing a one-character word leaves it unchanged.

Example 2:

Input: s = "ab"

Output: "ba"

Explanation: The word "ab" becomes "ba".

Example 3:

Input: s = "ab cd"

Output: "ba dc"

Explanation: Reverse "ab" to "ba" and "cd" to "dc", keeping the words in their original order and preserving the space.

Constraints:

  • 1 <= s.length <= 50000
  • All characters are printable ASCII. Words are separated by one space, with no leading or trailing spaces.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.