Skip to content

Instantly share code, notes, and snippets.

@dashsaurabh
Created January 25, 2020 01:41
Show Gist options
  • Save dashsaurabh/123fdb9a18eb1bb3850d5678ae9ec2d1 to your computer and use it in GitHub Desktop.
Save dashsaurabh/123fdb9a18eb1bb3850d5678ae9ec2d1 to your computer and use it in GitHub Desktop.
Reverse Words in a String
/**
* @param {string} s
* @return {string}
* "the sky is blue" should be printed as "blue is sky the"
*/
var reverseWords = function(s) {
let strArray = s.split(' ');
let result = []
let temp;
let n = strArray.length;
let stack = [];
for(let i = 0; i<n;i++) {
if (strArray[i] !== ""){
stack.push(strArray[i])
}
}
while (stack.length !== 0) {
result.push(stack.pop())
}
result = result.join(" ");
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment