Write a function that takes a sentence as input and reverses the order of the words in that sentence. The reversed sentence should maintain the original spacing between words and should not alter the characters within each word.
Input: str = "Hey Aman is that correct way to reverse words of a string"
Output: "string a of words reverse to way correct that is Aman Hey"
function reverseWordsUsingReduce(str) {
return str.split(" ").reduce((reversed, word) => word + " " + reversed, "").trim();
}function reverseWordsUsingSplitReverseJoin(str) {
return str.split(" ").reverse().join(" ");
}function reverseWordsUsingForLoopAndPush(str) {
const words = [];
let word = "";
for (let i = 0; i < str.length; i++) {
if (str[i] === " ") {
words.push(word);
word = "";
} else {
word += str[i];
}
}
words.push(word);
return words.reverse().join(" ");
}function reverseWordsUsingForOfLoop(str) {
const words = [];
let word = "";
for (const char of str) {
if (char === " ") {
words.push(word);
word = "";
} else {
word += char;
}
}
words.push(word);
return words.reverse().join(" ");
}function reverseWordsUsingSplitAndMap(str) {
const words = str.split(" ");
return words.map((word, index, array) => array[array.length - 1 - index]).join(" ");
}function reverseWordsUsingArrayFromAndForEach(str) {
const words = [];
Array.from(str).forEach((char) => {
if (char === " ") {
words.push(words.pop().trim());
words.push("");
} else {
if (words.length === 0) words.push("");
words[words.length - 1] += char;
}
});
words.push(words.pop().trim());
return words.reverse().join(" ");
}function reverseWordsUsingReplaceRegex(str) {
return str.replace(/(\S+)/g, (match, p1, offset, string) => {
const words = string.match(/\S+/g);
return words[words.length - 1 - words.indexOf(p1)];
});
}function reverseWordsUsingSplitFilterReduce(str) {
return str.split(" ")
.filter(word => word.length > 0)
.reduce((reversed, word) => word + " " + reversed, "")
.trim();
}function reverseWordsUsingStack(str) {
const stack = [];
let word = "";
for (let i = 0; i < str.length; i++) {
if (str[i] === " ") {
stack.push(word);
word = "";
} else {
word += str[i];
}
}
stack.push(word);
return stack.reverse().join(" ");
}function reverseWordsSlidingWindow(s) {
let result = "";
// Return an empty string if input is empty or just whitespace
if (s.length === 0 || s.trim() === "") {
return result;
}
// If the string is just one character in length, return it
if (s.length === 1) {
return s;
}
// Extract the words using the GetWords function
const words = getWords(s);
// Reassembly of the words
result = words.join(" ") + " ";
// Trim the trailing space before returning
return result.trimEnd();
}
function getWords(s) {
const words = [];
let i = s.length - 1, j = i;
while (i >= 0) {
// If current char is whitespace and the next is not
if (s[i] === ' ' && s[i - 1] !== ' ') {
j = i; // Mark end of word
}
// If current char is not whitespace and the next is whitespace
else if (s[i] !== ' ' && s[i - 1] === ' ') {
// Extract the word
if (j === s.length - 1 && s[j] !== ' ') {
words.push(s.substring(i));
} else {
words.push(s.substring(i, j));
}
}
// Handle the case when the last character is not whitespace
if (i === 0 && s[i] !== ' ') {
words.push(s.substring(i, j + 1));
}
i--;
}
return words;
}function reverseWordsUsingSplitSortJoin(str) {
const words = str.split(" ");
return words.sort((a, b) => words.length - words.indexOf(a) - (words.length - words.indexOf(b))).join(" ");
}function reverseWordsTwoPointer(str) {
const n = str.length;
let ans = "";
if (n === 0) return "";
let temp = "";
for (let i = 0; i < n; i++) {
if (str[i] !== ' ') {
temp += str[i];
}
if (str[i] === ' ' && temp.length !== 0) {
ans = ans.length !== 0 ? temp + " " + ans : temp;
temp = "";
}
}
// Handle the last word
if (temp.length !== 0) {
ans = ans.length !== 0 ? temp + " " + ans : temp;
}
return ans.trim(); // Trim any leading or trailing spaces
}function reverseWordsDivideConquer(s) {
const words = s.trim().split(/\s+/); // Split by whitespace and trim input
let reversed = "";
// Construct the reversed string by appending words in reverse order
for (let i = words.length - 1; i >= 0; i--) {
if (reversed) {
reversed += " "; // Add a space if not the first word
}
reversed += words[i];
}
return reversed;
}