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.
1 <= s.length <= 10^4s contains English letters (upper-case and lower-case), digits, and spaces ' '.s.Follow-up: If the string data type is mutable in your language, can you solve it in-place with O(1) extra space?
No developer has claimed the Python throne for LeetCode #151 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
class Solution:
def reverseWords(self, s: str) -> str:
words = [word for word in s.split(' ') if word]
return ' '.join(words[::-1])
No developer has claimed the Java throne for LeetCode #151 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
class Solution {
public String reverseWords(String s) {
var wordList = new ArrayList<String>();
var words = s.split(" ");
for (var word : words) {
if (!word.isEmpty()) {
wordList.add(word);
}
}
int left = 0;
int right = wordList.size() - 1;
while (left < right) {
Collections.swap(wordList, left, right);
left++;
right--;
}
return String.join(" ", wordList);
}
}
No developer has claimed the C++ throne for LeetCode #151 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
class Solution {
public:
string reverseWords(string s) {
istringstream iss(s);
string word;
vector<string> word_list;
// 1. Extract words from the string.
// The istringstream >> operator automatically handles
// multiple spaces between words and leading/trailing spaces.
while (iss >> word) {
word_list.push_back(word);
}
reverse(word_list.begin(), word_list.end());
// 2. Join the words with a single space.
string result = "";
result = word_list[0];
for (auto i = 1; i < word_list.size(); ++i) {
result += " ";
result += word_list[i];
}
return result;
}
};
No developer has claimed the Javascript throne for LeetCode #151 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
var reverseWords = function(s) {
const words = s.split(' ').filter((word) => word !== '');
return words.reverse().join(' ');
};
No developer has claimed the Go throne for LeetCode #151 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
func reverseWords(s string) string {
words := strings.Fields(s) // Fields splits on whitespace and ignores multiple spaces
// Reverse the words
for i, j := 0, len(words) - 1; i < j; {
words[i], words[j] = words[j], words[i]
i += 1
j -= 1
}
return strings.Join(words, " ")
}
No developer has claimed the C# throne for LeetCode #151 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
public class Solution {
public string ReverseWords(string s) {
// Split into words, remove empty entries, reverse
var words = s.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries).Reverse();
return string.Join(" ", words);
}
}
No developer has claimed the Ruby throne for LeetCode #151 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
def reverse_words(s)
s.split(' ').reject(&:empty?).reverse.join(' ')
end
No developer has claimed the Rust throne for LeetCode #151 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Swift throne for LeetCode #151 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Kotlin throne for LeetCode #151 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.