Question:

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example:

  1. Input: "Let's take LeetCode contest"
  2. Output: "s'teL ekat edoCteeL tsetnoc"

Solution:

/**
 * @param {string} s
 * @return {string}
 */
var reverseWords = function(s) {
  let arr = s.split(' ');

  for (let i = 0; i < arr.length; i++) {
    arr[i] = arr[i].split('').reverse().join('');
  }

  return arr.join(' ');
};

Runtime: 92 ms, faster than 33.72% of JavaScript online submissions for Reverse Words in a String III.