Ctrl K

Each Word Backwards

Easy
You are given a string sentence: words separated by exactly one space. A word is any non-empty run of characters without spaces. Reverse each word on its own, keeping the order of the words and the spaces between them. Return the resulting string.
Example 1
sentence='Keep it simple' → 'peeK ti elpmis'

Each word is read backwards while the word order stays: peeK ti elpmis.

Example 2
sentence='2 + 2 = 4' → '2 + 2 = 4'

Every "word" here is a single character, so the string is unchanged.

Example 3
sentence="don't-stop" → "pots-t'nod"

There are no spaces, so it is one word, reversed as a whole along with the apostrophe and the dash: pots-t'nod.

Constraints
  • 1 ≤ sentence.length ≤ 5 · 10⁴
  • sentence consists of printable ASCII characters
  • there are no leading or trailing spaces
  • words are separated by exactly one space, and there is at least one word
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWhere the Fragment Starts EasyMirror Phrase Check EasySame When Flipped EasyFlip the Characters in Place EasyMirror the Vowels Easy
loading Python…
Input
sentence='Keep it simple'
Expected
'peeK ti elpmis'

Run checks the first 3 tests in your browser. Submit checks all 86 tests on the server, hidden ones included.