LeetCode: Reverse Words in a String III

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"
Output: "s'teL ekat edoCteeL tsetnoc"

Solution #1

def reverse_words(s)
  result = []
  split_words = s.split(' ')

  split_words.each_with_index do |word, i|
    result << word.reverse
    if i < split_words.length-1
      result << " "
    end
  end

  result.join
end

Solution #2

def reverse_words(s)
     s.split.map(&:reverse).join(" ")
end