Reverse Words In A String

Easy
0/40
Average time to solve is 10m
profile
Contributed by
430 upvotes
Asked in companies
MakeMyTripOlaCognizant

Problem statement

You are given a string 'str' of length 'N'.


Your task is to reverse the original string word by word.


There can be multiple spaces between two words and there can be leading or trailing spaces but in the output reversed string you need to put a single space between two words, and your reversed string should not contain leading or trailing spaces.


Example :
If the given input string is "Welcome to Coding Ninjas", then you should return "Ninjas Coding to Welcome" as the reversed string has only a single space between two words and there is no leading or trailing space.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line of input contains a string that you need to reverse word by word.
Output Format :
Print the reversed string such that there should be only one space between two strings and there should not be any trailing space.
Note :
Do not print anything. It has already been taken care of.

If the string data type is immutable in your language, consider using a mutable data type as an alternative.
Sample Input 1 :
Welcome to Coding Ninjas
Sample Output 1:
Ninjas Coding to Welcome
Explanation For Sample Input 1:
You need to reduce multiple spaces between two words to a single space in the reversed string and observe how the multiple spaces, leading and trailing spaces have been removed.
Sample Input 2 :
I am a star
Sample Output 2:
star a am I
Explanation For Sample Input 2:
Your reversed string should not contain leading or trailing spaces.
Constraints :
0 <= N <= 10^5

Time Limit: 1 sec
Follow-up:
If the string data type is mutable in your language, can you solve it in place with O(1) extra space?
Hint

Reverse the string word by word and add it in the front of the answer string.

Approaches (2)
Brute force
  • Create a String ans to store the reversed string.
  • Initialize a variable i to 0 and iterate the whole string through a while loop.
  • Skip initial spaces by just incrementing i.
  • Create a String that will store the current word.
  • Add the currentword and space at the beginning of ans.
  • After traversing the whole string, check if the length of ans is greater than 0 then return ans after removing the last space otherwise return an empty string.
Time Complexity

O(N ^ 2), where N is the length of the string 

 

As for every word we are attaching in front of the String.

Space Complexity

O(N), where N is the length of the string

 

For storing the reversed string.

Video Solution
Unlock at level 3
(75% EXP penalty)
Code Solution
(100% EXP penalty)
Reverse Words In A String
Full screen
Console