Just [[currentUser.getNumFreeQuestionsLeft()]] No more free questions left!Upgrade Now
You're working on a secret team solving coded transmissions.
Your team is scrambling to decipher a recent message, worried it's a plot to break into a major European National Cake Vault. The message has been mostly deciphered, but all the words are backwards! Your colleagues have handed off the last step to you.
Write a function reverseWords that takes a string message and reverses the order of the words in-place.
Since strings in Java are immutable, we'll first convert the string into an array of characters, do the in-place word reversal on that array, and re-join that array into a string before returning it. This isn't technically "in-place" and the array of characters will cost additional space, but it's a reasonable way to stay within the spirit of the challenge. If you're comfortable coding in a language with mutable strings, that'd be even better!
When writing your function, assume the message contains only letters and spaces, and all words are separated by one space.
Are you sure you're operating on the array of characters in-place?
We can do this in time.
If you're swapping individual words one at a time, consider what happens when the words are different lengths. Isn't each swap time in the worst case?
time and space. Our space cost comes from converting the message string to an array. If our input was an array, our space cost would be because we'd be using a constant amount of additional space beyond the input.
Hmm, the team used your function to finish deciphering the message. There definitely seems to be a plot brewing, but no specifics on where. The sender seemed to really like Eccles cakes. Any ideas?
How would you handle punctuation?