Reverse Words in a String III

Given a string s of words separated by single spaces, reverse the characters of each word while keeping the words in their original order and the spaces in place. Return the resulting string.

30:00

Reverse Words in a String III
easy
Free written solution
Topics
Companies

Given a string s of words separated by single spaces, reverse the characters of each word while keeping the words in their original order and the spaces in place. Return the resulting string.

Example 1:
Input: {"s":"Let's take LeetCode contest"}
Output: s'teL ekat edoCteeL tsetnoc
Constraints:
  • 1≤s.length≤5×1041 \leq s.\text{length} \leq 5 \times 10^4

  • s contains printable ASCII characters.

  • s has no leading or trailing spaces.

  • s has at least one word, and words are separated by a single space.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"Let's take LeetCode contest"}

Copy s into a mutable array of 27 characters. It has 4 words, and start = 0 points at the first one.

original s

Let's take LeetCode contest

chars (reversed in place)

words done 0/4

L

0

.

e

1

.

t

2

.

'

3

.

s

4

.

␣

5

.

t

6

.

a

7

.

k

8

.

e

9

.

␣

10

.

L

11

.

e

12

.

e

13

.

t

14

.

C

15

.

o

16

.

d

17

.

e

18

.

␣

19

.

c

20

.

o

21

.

n

22

.

t

23

.

e

24

.

s

25

.

t

26

.

Variables
VariableValue
n27
start0
DepthFunction Call
Stack empty
0/24