




Sponsored
Sponsored
This approach involves splitting the given string into words, reversing each word individually, and then joining them back together.
Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1), as only a constant amount of extra space is used.
1def reverseWords(s):
2    words = s.split()
3    return ' '.join(word[::-1] for word in words)
4
5s = "Let's take LeetCode contest"
6print(reverseWords(s))In Python, we split the string into words, reverse each word, and then join them back together with spaces.
In this Python solution, the string is first converted into a list to allow in-place modifications. Then, a custom reverse function swaps the word's elements in-place.