Last updated on January 5th, 2025 at 01:20 am
Here, we see Reverse Words in a String LeetCode Solution. This Leetcode problem is solved using different approaches in many programming languages, such as C++, Java, JavaScript, Python, etc.
List of all LeetCode Solution
Topics
String
Companies
Apple, Bloomberg, Microsoft, Snapchat, Yelp
Level of Question
Medium
Reverse Words in a String LeetCode Solution
Table of Contents
1. Problem Statement
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.
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.
2. Coding Pattern Used in Solution
The coding pattern used in all the provided implementations is String Manipulation. This pattern involves processing strings to achieve a specific transformation or result, and focuses on reversing the order of words in a string, which is a classic string manipulation task.
3. Code Implementation in Different Languages
3.1 Reverse Words in a String 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; } };
3.2 Reverse Words in a String 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; } } }
3.3 Reverse Words in a String 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; };
3.4 Reverse Words in a String Python
class Solution(object): def reverseWords(self, s): return " ".join(s.strip().split()[::-1])
4. Time and Space Complexity
Time Complexity | Space Complexity | |
C++ | O(n) | O(n) |
Java | O(n) | O(n) |
JavaScript | O(n) | O(n) |
Python | O(n) | O(n) |