Created
January 25, 2020 01:41
-
-
Save dashsaurabh/123fdb9a18eb1bb3850d5678ae9ec2d1 to your computer and use it in GitHub Desktop.
Reverse Words in a String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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