< Back

557. Reverse Words in a String III

This is barely a LeetCode question, but we're documenting the solution, anyways. We're asked to
reverse every word in an array, but maintain their position in the array. With Python, this is pretty

The solution is as follows:

  class Solution:
      def reverseWords(self, s: str) -> str:
          return " ".join(word[::-1] for word in s.split())

_ Time Complexity:

  O(n) - We iterate through the entire array once.

_ Space Complexity:

  O(n) - I'm sure Python creates a new array in the background when processing this one-liner.