Learn to solve Leetcode 151. Reverse Words in a String with multiple approaches. Answer from Design Gurus on designgurus.io
🌐
LeetCode
leetcode.com › problems › reverse-words-in-a-string
Reverse Words in a String - LeetCode
Can you solve this real interview question? Reverse Words in a String - 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.
🌐
AlgoMonster
algo.monster › liteproblems › 151
151. Reverse Words in a String - In-Depth Explanation
In-depth solution and explanation for LeetCode 151. Reverse Words in a String in Python, Java, C++ and more. Intuitions, example walk through, and complexity analysis. Better than official and forum solutions.
🌐
Medium
medium.com › @keinn › lc-151-reverse-words-in-a-string-29d27284b519
LC 151. Reverse Words in a String | by Kein Li | Medium
November 10, 2023 - So we append a space after appending the word, except the last word which we should add a check for · I used StringBuilder since it is much more efficient than concatenating Strings in Java ... class Solution { public String reverseWords(String s) { String temp = s.trim(); String[] arr = temp.split(" "); StringBuilder sb = new StringBuilder(); for (int i = arr.length - 1; i >= 0; i--){ String str = arr[i]; if (str.isBlank()) continue; sb.append(str); if (i != 0) sb.append(" "); // add space for each character, except the last } return sb.toString(); } }
🌐
DEV Community
dev.to › rahulgithubweb › leetcode-challenge-151-reverse-words-in-a-string-javascript-solution-3j2
LeetCode Challenge: 151. Reverse Words in a String - JavaScript Solution 🚀 - DEV Community
December 24, 2024 - A word is defined as a sequence of non-space characters. Return the reversed string with a single space between the words.
🌐
edSlash
edslash.com › home › leetcode challenge #151. reverse words in a string
LeetCode Challenge #151. Reverse Words in a String - edSlash
December 3, 2023 - In this program, we have to reverse the order of words while keeping the word’s direction the same. Only the words will be interchanged. ... 1. We initialize an integer variable ‘i’ to the length of the input string minus one (the index of the last character in the string) and we also initialize two empty strings: ‘ans’ and ‘helper’.
🌐
GitHub
github.com › doocs › leetcode › blob › main › solution › 0100-0199 › 0151.Reverse Words in a String › README_EN.md
leetcode/solution/0100-0199/0151.Reverse Words in a String/README_EN.md at main · doocs/leetcode
Follow-up: If the string data type is mutable in your language, can you solve it in-place with O(1) extra space? ... $j$ to find each word, add it to the result list, then reverse the result list, and finally concatenate it into a string.
Author   doocs
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › reverse-words-in-a-given-string
Reverse words in a string - GeeksforGeeks
Reverse the entire string, then iterate through it to extract words separated by dots. Reverse each word individually and update the original string until the end is reached.
Published   October 29, 2025
Find elsewhere
🌐
LeetCode
leetcode.com › problems › reverse-words-in-a-string › description
151. Reverse Words in a String
Can you solve this real interview question? Reverse Words in a String - 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.
🌐
Stack Overflow
stackoverflow.com › questions › 71089044 › i-stuck-in-leetcode-problem-151-reverse-words-in-a-string
time complexity - i stuck in leetcode problem 151. Reverse Words in a String - Stack Overflow
class Solution { public: string reverseWords(string s) { string ans; int i =0; int n = s.size(); while(i<n) { while(i<n and s[i]==' ') i++; if(i>=n) break; int j =i+1; while(j<n and s[j]!=' ') j++; string word = s.substr(i,j-1); if(ans.size()==0) ans = word; else ans = word + " "+ ans; i = j+1; } return ans; } };
🌐
YouTube
youtube.com › watch
Reverse Words in a String - LeetCode 151 - Python #leetcode75 - YouTube
Explaining how to solve Reverse Words in a String from leetcode in Python! Code: https://github.com/deepti-talesra/LeetCode/blob/master/Reverse_Words_in_a_St...
Published   November 1, 2024
🌐
Medium
medium.com › @sheefanaaz6417 › 151-reverse-words-in-a-string-leetcode-step-by-step-approach-90de235ea9ef
151. Reverse Words in a String: Leetcode Step-by-Step Approach | by Sheefa Naaz | Medium
August 24, 2023 - Step 1: Reverse the Entire String - The input string `s` is reversed using the `reverse` function, effectively reversing the order of all characters in the string. Step 2: Initialize Pointers and Length - Initialize three integer variables: `i` (a current position pointer), `l` (start of a word pointer), and `r` (end of a word pointer).
🌐
GitHub
github.com › ShreyasKadiri › LeetCode › blob › master › 151. Reverse Words in a String.java
LeetCode/151. Reverse Words in a String.java at master · ShreyasKadiri/LeetCode
public String reverseWords(String s) { String[] words = s.trim().replaceAll(" +", " ").split(" "); StringBuilder reverse = new StringBuilder(""); for(int i=words.length-1; i>=0; i--){ reverse.append(words[i] + " "); } ·
Author   ShreyasKadiri
🌐
YouTube
youtube.com › watch
REVERSE WORDS IN A STRING | LEETCODE 151 | PYTHON SOLUTION - YouTube
In this video we are solving a popular Microsoft interview question: Reverse Words in a String.This is a medium level question on Leetcode but really it's qu...
Published   March 19, 2022
🌐
Reddit
reddit.com › r/leetcode › issue in mu code in --> 151. reverse words in a string
r/leetcode on Reddit: issue in mu code in --> 151. Reverse Words in a String
March 1, 2022 -

Im tryna solve this problem:

https://leetcode.com/problems/reverse-words-in-a-string/description/

with this code::

class Solution {
public:
    string reverseWords(string s) {
        stack <string> stk;
        string str;
        string word = "";

        for(int i =0 ;i < (int) s.size(); i++){
            if(s[i]!=' ') {
                word +=s[i];
            }else{
                stk.push(word);
                word = "";
            }
        }
        str = stk.top();
        stk.pop();
        while(!stk.empty()){
            str += ' ' + stk.top();
            stk.pop();
        }

        return str;
    }
};

s = "the sky is blue"

and the expected outcome is: "blue is sky the"
mine is :: "is sky the"

please help!

🌐
LeetCode
leetcode.ca › all › 151.html
Leetcode 151. Reverse Words in a String
Input: " hello world! " Output: "world! hello" Explanation: Your reversed string should not contain leading or trailing spaces. ... Input: "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.
🌐
Medium
medium.com › @cutesciuridae › study-note-medium-leetcode-151-reverse-words-in-a-string-386fb83e4c03
Study note: [Medium] Leetcode #151 Reverse Words in a String | by 可愛小松鼠 Cute Squirrel | Medium
January 3, 2020 - Split each token from input string, discard any leading or trailing white spaces, and store them in a container such as list. Reverse the token order in container. (Recall python [::-1] syntax) Concatenate each reversed token with a white space as separator. ... For more implementation detail, please refer to the code snippet as below. ... First, tokenization takes O( n ) to scan each word and split.