Skip to content

Instantly share code, notes, and snippets.

@Rutvik17
Created August 9, 2021 01:44
Show Gist options
  • Save Rutvik17/05bfa53e68ff7150b9dbb2a24a294fed to your computer and use it in GitHub Desktop.
Save Rutvik17/05bfa53e68ff7150b9dbb2a24a294fed to your computer and use it in GitHub Desktop.
Reverse Words In String LeetCode
/*
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.
*/
const reverseWords = (s) => {
s = s.trim();
s = s.split(' ');
let reversedArray = [];
for (let i = s.length - 1; i >= 0; i--) {
reversedArray.push(s[i]);
}
const indexToRemove = [];
for (let i = 0; i < reversedArray.length; i++) {
if (reversedArray[i] === '') {
indexToRemove.push(i);
}
}
for (let i = indexToRemove.length - 1; i >= 0; i--) {
reversedArray.splice(indexToRemove[i], 1);
}
console.log(reversedArray.join(' '));
return reversedArray.join(' ');
};
const s = "Alice does not even like bob";
reverseWords(s);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment