Back to problems

Reverse Words in a String (Preserve Spaces, In-Place)

Algorithm · ByteDance · Medium

Requirements For a supplied string s, return its words in reverse sequence. Treat any longest continuous stretch of characters other than spaces as one word. Follow-up prompts: Create several test cases for the initial implementation and execute them. Adapt the initial approach so every original space is retained exactly (each gap between word slots must contain the same number of spaces as before). Improve the complexity — start by optimizing your own approach, then discuss…

Checking your access…