DIY: Reverse Words in a String

Solve the interview question "Reverse Words in a String" in this lesson.

Problem statement

In this problem, you are given a string,s. our task is to reverse the order of the words present in s. The string may have:

  • Words that are separated by more than one space.
  • Leading and trailing spaces.

You also have to remove the extra spaces between words and trim the leading and trailing spaces. So, you will return a string with words in a reverse order and with one space between each of them.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.