Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Reverse Words in String Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Reverse Words in String | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given a string s, reverse the order of words. A word is a sequence of non-space characters. Words in the string s will have one space between them. There are no leading or trailing spaces.

Approach

The problem can be solved by traversing the string and store all the words in an array of strings in the same sequence. After storing all the words traverse the array from end to start and store all the words in the answer string separated by a space.

Analysis

  • Time Complexity: O(n)
  • Auxiliary Space Complexity: O(n)

Implementation

C++
string reverseWords(string &s) {
   vector<string> allWords;
   string currentWord = "";
   for(int i = 0; i < s.size(); i++) {
       if(s[i] == ' ') {
           allWords.push_back(currentWord);
           currentWord = "";
       } else {
           currentWord += s[i];
       }
   }
   allWords.push_back(currentWord);
   string ans;
   for(int i = allWords.size() - 1; i > 0; i--) {
       ans += allWords[i];
       ans += ' ';
   }
   ans += allWords[0];
   return ans;
}
Java
class Solution {
   String reverseWords(String s) {
       int numOfWords = 1;
       for (int i = 0; i < s.length(); i++) {
           if(s.charAt(i) == ' ') {
               numOfWords++;
           }
       }
       String[] allWords = new  String[numOfWords];
       String currentWord = "";
       int index = 0;
       for(int i = 0; i < s.length(); i++) {
           if(s.charAt(i) == ' ') {
               allWords[index++] = currentWord;
               currentWord = "";
           } else {
               currentWord += s.charAt(i);
           }
       }
       allWords[index++] = currentWord;
       String ans = "";
       for(int i = index - 1; i > 0; i--) {
           ans += allWords[i];
           ans += ' ';
       }
       ans += allWords[0];
       return ans;
   }
}

Another Approach

This approach has the same time complexity as the previous approach. The idea here is to first reverse the whole string and then again reverse each of the words.

  • First, reverse the whole string.
  • Assign a pointer to the beginning of the string.
  • Traverse the string from i = 0 to i = n - 1 and as soon as space is encountered reverse the string up to that position and then assign the pointer to the next index (i + 1).

Analysis

  • Time Complexity: O(n)
  • Auxiliary Space Complexity: O(1)

Implementation

C++
string reverseWords(string &s) {
   int n = s.size();
     for(int i = 0; i < n/2; i++) {
       swap(s[i], s[n - 1 - i]);
   }
   int j = 0;
   for (int i = 0; i < n; i++) {
       if(s[i] == ' ') {
           int start = j, end = i - 1;
           while (j <= (start + end) / 2) {
               swap(s[j], s[end - (j - start)]);
               j++;
           }
           j = i + 1;
       }
   }
   int start = j, end = n - 1;
   while(j <= (start + end) / 2) {
       swap(s[j], s[end - (j - start)]);
       j++;
   }
   return s;
}
Java
class Solution {
   String reverseWords(String s) {
       int n = s.length();
       char[] str = new char[n];
       for (int i = 0; i < n; i++) {
           str[i] = s.charAt(i);
       }
       for(int i = 0; i < n/2; i++) {
           char temp = str[i];
           str[i] = str[n - 1 - i];
           str[n - 1 - i] = temp;
       }
       int j = 0;
       for (int i = 0; i < n; i++) {
           if(str[i] == ' ') {
               int start = j, end = i - 1;
               while (j <= (start + end) / 2) {
                   char temp = str[j];
                   str[j] = str[end - (j - start)];
                   str[end - (j - start)] = temp;
                   j++;
               }
               j = i + 1;
           }
       }
       int start = j, end = n - 1;
       while(j <= (start + end) / 2) {
           char temp = str[j];
           str[j] = str[end - (j - start)];
           str[end - (j - start)] = temp;
           j++;
       }
       s = "";
       for (int i = 0; i < n; i++) {
           s += str[i];
       }
       return s;
   }
}
Related Content
Anagrams
Compare Version Numbers
Count And Say
Integer to Roman Numeral
Longest Common Prefix
Longest Palindrome in String
Insert Minimum To Make Palindrome
Roman Numeral to Integer
Substring Search - I
Substring Search - III
Substring Search - II
Substring Search - IV
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet