415. Reverse Words in a String III
You are given a sentence s: words made of printable ASCII characters (no spaces inside a word), separated by single spaces, with no leading or trailing spaces.
Flip the characters of each word individually while keeping the words themselves in their original left-to-right order — the spaces stay exactly where they were.
Your function receives the string s and returns the transformed string.
Example 1:
Input: s = "keep calm and code on"
Output: "peek mlac dna edoc no"
Explanation: Each of the five words is reversed in place: "keep" becomes "peek", "calm" becomes "mlac", and so on — but the words stay in the same order.
Example 2:
Input: s = "racecar pop"
Output: "racecar pop"
Explanation: Both words are palindromes, so reversing them changes nothing.
Constraints:
- 1 ≤ s.length ≤ 5 * 10⁴
- s consists of printable ASCII characters and contains at least one word.
- Words are separated by a single space; there are no leading or trailing spaces.
Hints:
The easy route: split the sentence on spaces, reverse each piece, and glue the pieces back together with single spaces.
To do it without building word copies, scan for each word's boundaries and reverse that slice in place with two pointers — one at each end, swapping and walking inward until they meet.
▶ Run checks these sample cases. Submit also runs hidden edge cases.
Input: s = "keep calm and code on"
Expected output: "peek mlac dna edoc no"