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"



Note:
In the string, each word is separated by single space and there will not be any extra space in the string.

Source: leetcode | Difficulty: EASY
Tags: String algorithms

Amortized analysis

Starter code: class Solution:
    def reverseWords(self, s: str) -> str:
        

Solutions:
class Solution:

	def reverseWords(self, s):
		rev_str = s[::-1]
		rev_arr = rev_str.split()
		final = rev_arr[::-1]
		return ' '.join(map(str, final))


class Solution:

	def reverseWords(self, s):
		s = list(s.split())
		a = ''
		for i in range(len(s)):
			if i + 1 < len(s):
				for j in range(len(s[i]) - 1, -1, -1):
					a += s[i][j]
				a += ' '
			else:
				for j in range(len(s[i]) - 1, -1, -1):
					a += s[i][j]
		return a


class Solution:

	def reverseWords(self, s):
		s = list(s.split())
		a = ''
		for i in range(len(s)):
			if i + 1 < len(s):
				s[i] = list(s[i])
				start = 0
				end = len(s[i]) - 1
				while start < end:
					temp = s[i][start]
					s[i][start] = s[i][end]
					s[i][end] = temp
					start += 1
					end -= 1
				s[i] = ''.join(s[i])
				a += s[i]
				a += ' '
			else:
				s[i] = list(s[i])
				start = 0
				end = len(s[i]) - 1
				while start < end:
					temp = s[i][start]
					s[i][start] = s[i][end]
					s[i][end] = temp
					start += 1
					end -= 1
				s[i] = ''.join(s[i])
				a += s[i]
		return a


class Solution:

	def reverseWords(self, s):
		return ' '.join((word[::-1] for word in s.split()))


class Solution:

	def reverseWords(self, s):
		print(self.spaceindex(s))
		result = ''
		z = self.spaceindex(s)
		print(z)
		for i in range(0, len(z) - 1):
			word = s[z[i] + 1:z[i + 1]]
			result = result + word[::-1] + ' '
		return result + s[z[len(z) - 1] + 1:len(s)][::-1]

	def spaceindex(self, s):
		result = [-1]
		for i in range(0, len(s)):
			if s[i] == ' ':
				result.append(i)
		return result


class Solution:

	def reverseWords(self, s):
		res = ''
		words = list(s.split(' '))
		for word in words:
			for i in range(len(word)):
				res += word[len(word) - i - 1]
			res += ' '
		return res.strip()


class Solution:

	def reverseWords(self, s):
		words = s.split(' ')
		output = []
		for i in words:
			output += [''.join(reversed(i))]
		return ' '.join(output)


class Solution:

	def reverseWords(self, s):
		tmp = s.split()
		return ' '.join((i[::-1] for i in tmp))


class Solution:

	def reverseWords(self, s):
		words = s.split(' ')
		output = ''
		for (i, word) in enumerate(words):
			output += word[::-1]
			if i != len(words) - 1:
				output += ' '
		return output


class Solution:

	def reverseWords(self, s):
		buffer = ''
		s += ' '
		while len(s) - 1 > 0:
			space_position = s.find(' ')
			buffer += s[space_position - 1::-1]
			buffer += ' '
			s = s[space_position + 1:len(s)]
		buffer = buffer[:len(buffer) - 1]
		return buffer


class Solution:

	def reverseWords(self, s):
		return ' '.join(s.split(' ')[::-1])[::-1]


class Solution:

	def reverseWords(self, s):
		words = s.split(' ')
		result = list()
		for word in words:
			new_word = ''.join(reversed(list(word)))
			result.append(new_word)
		return ' '.join(result)


class Solution:

	def reverseWords(self, s):
		if len(s) < 2:
			return s
		words = s.split(' ')
		for (i, word) in enumerate(words):
			(l, m) = (0, len(word) - 1)
			w = list(word)
			while l < m:
				(w[l], w[m]) = (w[m], w[l])
				l += 1
				m -= 1
			words[i] = ''.join(w)
		return ' '.join(words)