Reverse Words in a String LeetCode Solution

Here, We see Reverse Words in a String LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Reverse Words in a String LeetCode Solution

Reverse Words in a String LeetCode Solution

Problem Statement

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

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.

Example 1:
Input: s = “the sky is blue”
Output: “blue is sky the”

Example 2:
Input: s = ” hello world ”
Output: “world hello”
Explanation: Your reversed string should not contain leading or trailing spaces.

Example 3:
Input: s = “a good example”
Output: “example good a”
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.

Reverse Words in a String LeetCode Solution C++

class Solution {
public:
    string reverseWords(string s) {
        if(s.size() == 0) return s;
        stack<string> stack;
        string result;
        for(int i=0; i<s.size(); i++) {
            string word;
            if(s[i]==' ') continue;
            while(i<s.size() && s[i]!=' ' ) {
                word += s[i]; i++;
            }
            stack.push(word);
        }
        while(!stack.empty()) {
            result += stack.top(); stack.pop();
            if(!stack.empty()) result += " ";
        }
        return result;        
    }
};Code language: PHP (php)

Reverse Words in a String LeetCode Solution Java

class Solution {
    public String reverseWords(String s) {
        if (s == null) return null;
        char[] a = s.toCharArray();
        int n = a.length;
        reverse(a, 0, n - 1);
        reverseWords(a, n);
        return cleanSpaces(a, n);
    }
    void reverseWords(char[] a, int n) {
        int i = 0, j = 0;
        while (i < n) {
        while (i < j || i < n && a[i] == ' ') i++;
        while (j < i || j < n && a[j] != ' ') j++;
        reverse(a, i, j - 1);
        }
    }
    
    String cleanSpaces(char[] a, int n) {
        int i = 0, j = 0;
        while (j < n) {
        while (j < n && a[j] == ' ') j++;
        while (j < n && a[j] != ' ') a[i++] = a[j++];
        while (j < n && a[j] == ' ') j++;
        if (j < n) a[i++] = ' ';
        }
        return new String(a).substring(0, i);
    }
    
    private void reverse(char[] a, int i, int j) {
        while (i < j) {
        char t = a[i];
        a[i++] = a[j];
        a[j--] = t;
        }        
    }
}Code language: JavaScript (javascript)

Reverse Words in a String Solution JavaScript

 var reverseWords = function(s) {
    let i = 0, j = s.length - 1;
    while (i <= j && s[i] === ' ') i++;
    while (j >= i && s[j] === ' ') j--;
    s = s.substring(i, j + 1);
    let words = s.split(/\s+/);
    let out = '';
    for (let k = words.length - 1; k > 0; k--) {
        out += words[k] + ' ';
    }
    out += words[0];
    return out;
};
Code language: JavaScript (javascript)

Reverse Words in a String Solution Python

class Solution(object):
    def reverseWords(self, s):
        return " ".join(s.strip().split()[::-1])Code language: CSS (css)
Scroll to Top