Reverse Words in a String III

EasyStringstwo pointers

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given a string s that contains words separated by single spaces, reverse the characters of **each individual word** while keeping the words in their original order. The spacing between words must be preserved exactly. In other words, you flip every word in place. A word that reads "hello" becomes "olleh", but its position in the sentence does not change. You may assume s contains no leading or trailing spaces and that exactly one space separates any two adjacent words. Return the resulting string.

Examples

Input: s = "Let us code"
Output: "teL su edoc"
Each word is reversed individually: "Let" -> "teL", "us" -> "su", "code" -> "edoc". The word order stays the same.
Input: s = "abc"
Output: "cba"
A single word is simply reversed.
Input: s = "a b c d"
Output: "a b c d"
Every word is one character long, so reversing a word leaves it unchanged.

Constraints

  • 1 <= s.length <= 5 * 10^4
  • `s` contains printable ASCII characters and spaces.
  • `s` does not contain leading or trailing spaces.
  • All words in `s` are separated by exactly one space.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More two pointers problems