Skip to content

Instantly share code, notes, and snippets.

@mihailgaberov
Created February 23, 2023 09:54
Show Gist options
  • Save mihailgaberov/1afd0df5588c3d6457dc45da85e5c1b4 to your computer and use it in GitHub Desktop.
Save mihailgaberov/1afd0df5588c3d6457dc45da85e5c1b4 to your computer and use it in GitHub Desktop.
// This problem was asked by Google.
// Given a string of words delimited by spaces, reverse the words in string. For example, given "hello world here", return "here world hello"
// Follow-up: given a mutable string representation, can you perform this operation in-place?
const reverseWords = (str) => str.split(' ').reverse().join(' ')
console.log(reverseWords("hello world here") === "here world hello") // true
console.log(reverseWords("hello world here") === "hello world here") // false
console.log(reverseWords("who the hell are you") === "you are hell the who") // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment