carsonSgit / leetcode

leetcode grind... doing whatever I can to make sure I'm not bad at CS.
4 stars 0 forks source link

Q151 #54

Closed carsonSgit closed 1 month ago

carsonSgit commented 1 month ago

What is this problem

Given an input string s, reverse the order of the words.

A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.

Return a string of the words in reverse order concatenated by a single space.

Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.

How do I solve it

Breakdown:

  1. Reverse the string
  2. Split at white spaces
  3. Append each iterable item + a white space
  4. Return new answer

Resources