Reverse Each Word in a Sentence

viaGlassdoor

Problem: Given a paragraph/sentence, reverse each word in place while keeping the word order unchanged.

Constraints: Input may contain multiple spaces or punctuation depending on follow-up.

Example: "Hello World" -> "olleH dlroW".

Approach: Split the string into words, reverse the characters of each word individually (two-pointer swap), then rejoin with the original spacing. O(n) time, O(n) space (or in-place per-word with O(1) extra space if using a mutable char array).

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account