loading

Reverse Words in a String — Step-by-Step Visualization

mediumLeetCode #151StringTwo Pointers

Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. Return a string of the words in reverse order concatenated by a single space.

Algorithm Pattern

Split + Reverse

Key Idea

Split words, filter empties, reverse the list, rejoin.

Step-by-Step Approach

  1. Split string by spaces
  2. Filter out empty strings
  3. Reverse the word list
  4. Join with single space

Related Problems