Given a string, your task is to reverse the string keeping the spaces intact to their positions.
Example 1:
Input:
S = "Help others"
Output: sreh topleH
Explanation: The space is intact at index 4
while all other characters are reversed.
Example 2:
Input: 
S = "geeksforgeeks"
Output: skeegrofskeeg
Explanation: No space present, hence the
entire string is reversed.
Your Task:
You don't need to read input or print anything. Your task is to complete the function reverseWithSpacesIntact() which takes the string S as input and returns the resultant string by reversing the string while keeping all the spaces intact.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(1).
Constraints:
1<=|S|<=10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Amortized analysis

Starter code: #User function Template for python3



class Solution:

    def reverseWithSpacesIntact(self, s):

        # code here

Solutions:
class Solution:

	def reverseWithSpacesIntact(self, s):
		l = []
		for i in range(len(s)):
			if s[i] == ' ':
				l.append(i)
		s = s[::-1].replace(' ', '')
		if len(l) == 0:
			return s
		j = 0
		for i in range(len(s) + len(l)):
			if i == l[j]:
				s = s[:i] + ' ' + s[i:]
				j += 1
			if j >= len(l):
				break
		return s


class Solution:

	def reverseWithSpacesIntact(self, s):
		result = list(s)
		start = 0
		end = len(s) - 1
		while start <= end:
			if s[start] != ' ' and s[end] != ' ':
				(result[start], result[end]) = (result[end], result[start])
				start += 1
				end -= 1
			elif s[start] == ' ' and s[end] == ' ':
				start += 1
				end -= 1
			elif s[start] == ' ':
				start += 1
			elif s[end] == ' ':
				end -= 1
		return ''.join(result)


class Solution:

	def reverseWithSpacesIntact(self, s):
		c = ''
		k = []
		for i in range(len(s)):
			if s[i] != ' ':
				c = c + s[i]
		c = c[::-1]
		g = ''
		i = 0
		j = 0
		while i < len(s):
			if s[i] != ' ':
				g = g + c[j]
			if s[i] == ' ':
				g = g + ' '
				j = j - 1
			i = i + 1
			j = j + 1
		return g


class Solution:

	def reverseWithSpacesIntact(self, s):
		stack = []
		for i in s:
			if i != ' ':
				stack.append(i)
		str = ''
		for i in s:
			if i != ' ':
				str = str + stack.pop()
			else:
				str = str + ' '
		return str


class Solution:

	def reverseWithSpacesIntact(self, s):
		n = len(s)
		s = [i for i in s]
		(i, j) = (0, n - 1)
		while i < j:
			if s[i] != ' ' and s[j] != ' ':
				(s[i], s[j]) = (s[j], s[i])
				i += 1
				j -= 1
			elif s[i] == ' ':
				i += 1
			elif s[j] == ' ':
				j -= 1
		return ''.join(s)


class Solution:

	def reverseWithSpacesIntact(self, s):
		res = []
		for i in s:
			res.append(i)
		low = 0
		high = len(s) - 1
		while low < high:
			if res[low] == ' ' and res[high] != ' ':
				low += 1
			elif res[low] != ' ' and res[high] == ' ':
				high -= 1
			else:
				(res[low], res[high]) = (res[high], res[low])
				low += 1
				high -= 1
		return ''.join(res)


class Solution:

	def reverseWithSpacesIntact(self, s):
		rev = s.replace(' ', '')
		rev = [i for i in rev]
		res = ''
		for j in s:
			if j != ' ':
				res += rev.pop()
			else:
				res += ' '
		return res


class Solution:

	def reverseWithSpacesIntact(self, s):
		if ' ' not in s:
			return s[::-1]
		else:
			arr = []
			for ele in s:
				arr.append(ele)
			low = 0
			high = len(arr) - 1
			while low < high:
				if arr[low] == ' ' and arr[high] != ' ':
					low = low + 1
				elif arr[low] == ' ' and arr[high] == ' ':
					low = low + 1
					high = high - 1
				elif arr[low] != ' ' and arr[high] == ' ':
					high = high - 1
				else:
					(arr[low], arr[high]) = (arr[high], arr[low])
					low = low + 1
					high = high - 1
		return ''.join(arr)


class Solution:

	def reverseWithSpacesIntact(self, s):
		a = ''
		j = len(s) - 1
		i = 0
		while i < len(s) and j >= 0:
			if s[i] == ' ':
				a += s[i]
				i += 1
				continue
			if s[j] != ' ':
				a += s[j]
				i += 1
			j -= 1
		return a


class Solution:

	def reverseWithSpacesIntact(self, s):
		rev = s.replace(' ', '')
		rev = [x for x in rev]
		res = ''
		for lett in s:
			if lett != ' ':
				res += rev.pop()
			else:
				res += ' '
		return res


class Solution:

	def reverseWithSpacesIntact(self, s):
		sList = list(s)
		i = 0
		j = len(sList) - 1
		while i <= j:
			if sList[i] != ' ' and sList[j] != ' ':
				(sList[i], sList[j]) = (sList[j], sList[i])
				i += 1
				j -= 1
			elif sList[i] == ' ':
				i += 1
			else:
				j -= 1
		return ''.join(sList)


class Solution:

	def reverseWithSpacesIntact(self, s):
		p = ''
		for i in range(len(s)):
			if s[-(i + 1)] != ' ':
				p += s[-(i + 1)]
		l = len(p)
		z = 0
		y = ''
		for i in range(len(s)):
			if s[i] != ' ':
				y += p[z]
				z += 1
			else:
				y += ' '
		return y


class Solution:

	def reverseWithSpacesIntact(self, s):
		space_indexes = [i for i in range(len(s)) if s[i] == ' ']
		if len(space_indexes) != 0:
			rev_sre = s.replace(' ', '')[::-1]
			lst = list(rev_sre)
			for i in range(len(space_indexes)):
				lst.insert(space_indexes[i], ' ')
			return ''.join(lst)
		else:
			lst = list(s)
			lst.reverse()
			return ''.join(lst)


class Solution:

	def reverseWithSpacesIntact(self, s):
		a = list(s)
		i = 0
		temp = 'a'
		j = len(a) - 1
		b = ''
		while i < j:
			if a[i] == ' ':
				i += 1
				continue
			elif a[j] == ' ':
				j -= 1
				continue
			else:
				temp = a[i]
				a[i] = a[j]
				a[j] = temp
			i += 1
			j -= 1
		for i in a:
			b = b + str(i)
		return b


class Solution:

	def reverseWithSpacesIntact(self, s):
		a = [x for x in s]
		i = 0
		j = len(s) - 1
		while i < j:
			if a[i] == ' ':
				i = i + 1
				continue
			if a[j] == ' ':
				j = j - 1
				continue
			elif a[i] != ' ' and a[j] != ' ':
				(a[i], a[j]) = (a[j], a[i])
				i = i + 1
				j = j - 1
		return ''.join(a)


class Solution:

	def reverseWithSpacesIntact(self, s):
		stt = ''
		left = 0
		right = len(s) - 1
		while right >= 0:
			if s[right] == ' ':
				right -= 1
				continue
			elif s[left] == ' ':
				stt += ' '
				left += 1
			else:
				stt += s[right]
				right -= 1
				left += 1
		return stt


class Solution:

	def reverseWithSpacesIntact(self, s):
		t = s
		d = ''.join(t.split())
		d = d[::-1]
		d = list(d)
		l = []
		for i in range(len(s)):
			if s[i] == ' ':
				l.append(i)
		for i in l:
			d.insert(i, ' ')
		return ''.join(d)


class Solution:

	def reverseWithSpacesIntact(self, s):
		t = s
		k = ''.join(s.split())
		l = [i for i in k]
		l = l[::-1]
		ind = []
		for i in range(len(t)):
			if t[i] == ' ':
				ind.append(i)
		for i in ind:
			l.insert(i, ' ')
		return ''.join(l)


class Solution:

	def reverseWithSpacesIntact(self, s):
		j = 0
		n = len(s)
		output = ''
		for i in range(n):
			while j < n and s[-j - 1] == ' ':
				j += 1
			if s[i] == ' ':
				output += ' '
			else:
				output += s[-j - 1]
				j += 1
		return output


class Solution:

	def reverseWithSpacesIntact(self, s):
		a = []
		s = list(s)
		for (i, v) in enumerate(s):
			if v == ' ':
				a.append(i)
				s[i] = ''
		s = ''.join(s)[::-1]
		s = list(s)
		for v in a:
			s.insert(v, ' ')
		return ''.join(s)


class Solution:

	def reverseWithSpacesIntact(self, s):
		rvStr = ''
		spaceCount = 0
		for i in range(len(s) - 1, -1, -1):
			if s[i] != ' ':
				rvStr += s[i]
			else:
				spaceCount += 1
		if spaceCount > 0:
			for i in range(len(s)):
				if s[i] == ' ':
					rvStr = rvStr[0:i] + ' ' + rvStr[i:]
		return rvStr


class Solution:

	def reverseWithSpacesIntact(self, s):
		k = []
		l = []
		for i in range(len(s)):
			if s[i] == ' ':
				l.append(i)
			else:
				k.append(s[i])
		k.reverse()
		for i in range(len(l)):
			k.insert(l[i], ' ')
		return ''.join(k)


class Solution:

	def reverseWithSpacesIntact(self, s):
		arr = [i for i in s]
		start = 0
		end = len(arr) - 1
		while start < end:
			if arr[start] == ' ':
				start += 1
			elif arr[end] == ' ':
				end -= 1
			else:
				temp = arr[start]
				arr[start] = arr[end]
				arr[end] = temp
				start += 1
				end -= 1
		return ''.join((i for i in arr))


class Solution:

	def reverseWithSpacesIntact(self, s):
		l = list(s)
		i = 0
		j = len(l) - 1
		while i <= j:
			if l[i].isalpha() == False:
				i += 1
			elif l[j].isalpha() == False:
				j -= 1
			else:
				(l[i], l[j]) = (l[j], l[i])
				i += 1
				j -= 1
		return ''.join(l)


class Solution:

	def reverseWithSpacesIntact(self, s):
		n = len(s)
		rev = []
		for i in s[::-1]:
			if i != ' ':
				rev.append(i)
		spaces = s.count(' ')
		for i in range(n):
			if spaces and s[i] == ' ':
				rev.insert(i, ' ')
				spaces -= 1
		return ''.join(rev)


class Solution:

	def reverseWithSpacesIntact(self, s):
		res = list(s)
		leng = len(s)
		i = 0
		j = leng - 1
		while i <= j:
			if s[i] != ' ' and s[j] != ' ':
				(res[i], res[j]) = (res[j], res[i])
				i += 1
				j -= 1
			elif s[i] == ' ' and s[j] != ' ':
				i += 1
			elif s[i] != ' ' and s[j] == ' ':
				j -= 1
			elif s[i] == ' ' and s[j] == ' ':
				i += 1
				j -= 1
		return ''.join(res)


class Solution:

	def reverseWithSpacesIntact(self, st):
		n = len(st)
		res = list(st)
		start = 0
		end = n - 1
		while start < end:
			if res[start] == ' ':
				start += 1
				continue
			elif res[end] == ' ':
				end -= 1
				continue
			else:
				(res[start], res[end]) = (res[end], res[start])
				start += 1
				end -= 1
		return ''.join(res)


class Solution:

	def reverseWithSpacesIntact(self, s):
		a = ''.join(list(s)[::-1]).replace(' ', '')
		a = list(a)
		for i in range(len(s)):
			if s[i] == ' ':
				a.insert(i, ' ')
		return ''.join(a)


class Solution:

	def reverseWithSpacesIntact(self, s):
		start = 0
		end = len(s) - 1
		s = list(s)
		while start < end:
			if s[start] == ' ':
				start += 1
				continue
			if s[end] == ' ':
				end -= 1
				continue
			else:
				(s[start], s[end]) = (s[end], s[start])
				start += 1
				end -= 1
		return ''.join(s)


class Solution:

	def reverseWithSpacesIntact(self, s):
		ans = []
		for i in s:
			ans.append(i)
		one = 0
		two = len(s) - 1
		while one < two:
			if ans[one] == ' ':
				one += 1
			elif ans[two] == ' ':
				two -= 1
			else:
				(ans[one], ans[two]) = (ans[two], ans[one])
				one += 1
				two -= 1
		return ''.join(ans)


class Solution:

	def reverseWithSpacesIntact(self, s):
		r = list(s)
		h = 0
		e = len(s) - 1
		while h <= e:
			if s[h] != ' ' and s[e] != ' ':
				(r[h], r[e]) = (r[e], r[h])
				h += 1
				e -= 1
			elif s[h] == ' ' and s[e] == ' ':
				h += 1
				e -= 1
			elif s[h] == ' ':
				h += 1
			elif s[e] == ' ':
				e -= 1
		return ''.join(r)


class Solution:

	def reverseWithSpacesIntact(self, s):
		ch = list(s)
		i = 0
		j = len(s) - 1
		while i <= j:
			if ch[i] == ' ':
				i += 1
				continue
			if ch[j] == ' ':
				j -= 1
				continue
			temp = ch[i]
			ch[i] = ch[j]
			ch[j] = temp
			i += 1
			j -= 1
		res = ''
		for i in ch:
			res += i
		return res


class Solution:

	def reverseWithSpacesIntact(self, s):
		h = s[::-1]
		k = ''
		for i in h:
			if i != ' ':
				k += i
		k = list(k)
		t = ''
		for i in s:
			if i == ' ':
				t += ' '
			else:
				t += k.pop(0)
		return t


class Solution:

	def reverseWithSpacesIntact(self, s):
		size = len(s) - 1
		new_str1 = ''
		arr = s
		i = 0
		while size >= 0:
			if i < len(s) and arr[i] == ' ':
				new_str1 += ' '
				i += 1
			else:
				if arr[size] != ' ':
					new_str1 += arr[size]
					i += 1
				size -= 1
		return new_str1


class Solution:

	def reverseWithSpacesIntact(self, s):
		(l, r) = (0, len(s) - 1)
		s = list(s)
		while l < r:
			if s[l] == ' ':
				l += 1
			elif s[r] == ' ':
				r -= 1
			else:
				(s[l], s[r]) = (s[r], s[l])
				l += 1
				r -= 1
		return ''.join(s)


class Solution:

	def reverseWithSpacesIntact(self, s):
		n = len(s)
		characters = []
		for char in s:
			if char != ' ':
				characters.append(char)
		characters.reverse()
		ans = [-1 for i in range(len(s))]
		for i in range(n):
			if s[i] == ' ':
				ans[i] = ' '
			else:
				ans[i] = characters.pop(0)
		ans = ''.join(ans)
		return ans


class Solution:

	def reverseWithSpacesIntact(self, s):
		res = ''
		rev = s[::-1].replace(' ', '')
		i = 0
		j = 0
		while len(s) > i or len(rev) > j:
			if len(s) > i and s[i] == ' ':
				res += ' '
			elif len(rev) > j:
				res += rev[j]
				j += 1
			i += 1
		return res


class Solution:

	def reverseWithSpacesIntact(self, s):
		i = len(s) - 1
		k = list(s)
		j = 0
		while j < i:
			if k[j] == ' ':
				j += 1
				continue
			elif k[i] == ' ':
				i -= 1
				continue
			else:
				(k[j], k[i]) = (k[i], k[j])
				j += 1
				i -= 1
		return ''.join(k)


class Solution:

	def reverseWithSpacesIntact(self, s):
		new = [i for i in s[::-1] if i != ' ']
		for i in range(len(s)):
			if s[i] == ' ':
				new.insert(i, ' ')
		return ''.join(new)


class Solution:

	def reverseWithSpacesIntact(self, s):
		st = [x for x in s if x != ' ']
		st = st[::-1]
		r = []
		for x in range(len(s)):
			if s[x] == ' ':
				r.append(x)
		for x in r:
			st.insert(x, ' ')
		st = ''.join(st)
		return st


class Solution:

	def reverseWithSpacesIntact(self, s):
		l = []
		s1 = ''
		for (j, i) in enumerate(s):
			if i == ' ':
				l.append(j)
			else:
				s1 += i
		r = ''
		j = len(s1) - 1
		i = 0
		while j >= 0:
			if l and i == l[0]:
				l.pop(0)
				i += 1
				r += ' '
			else:
				r = r + s1[j]
				i += 1
				j -= 1
		return r


class Solution:

	def reverseWithSpacesIntact(self, s):
		s1 = s.replace(' ', '')
		s2 = s1[::-1]
		ls = [0] * len(s)
		for i in range(len(s)):
			if s[i] == ' ':
				ls[i] = 1
		j = 0
		res = ''
		for i in range(len(ls)):
			if ls[i] == 1:
				res += ' '
			else:
				res += s2[j]
				j += 1
		return res


class Solution:

	def reverseWithSpacesIntact(self, s):
		x = s.split()
		y = ''
		for i in x:
			for j in i:
				y += j
		y = y[::-1]
		ans = ''
		a = 0
		b = 0
		while a < len(s):
			if s[a].isalpha():
				ans += y[b]
				a += 1
				b += 1
			else:
				ans += ' '
				a += 1
		return ans


class Solution:

	def reverseWithSpacesIntact(self, s):
		t = []
		for x in s:
			if x != ' ':
				t.append(x)
		t = t[::-1]
		u = ''
		j = 0
		for i in range(len(s)):
			if s[i] == ' ':
				u += ' '
			else:
				u += t[j]
				j += 1
		return u
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		ob = Solution()
		ans = ob.reverseWithSpacesIntact(s)
		print(ans)


class Solution:

	def reverseWithSpacesIntact(self, s):
		ind = [i for (i, x) in enumerate(s) if x == ' ']
		if len(ind) > 0:
			rev_s = s[::-1].replace(' ', '')
			for i in ind:
				rev_s = rev_s[:i] + ' ' + rev_s[i:]
			return rev_s
		else:
			return s[::-1]
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		ob = Solution()
		ans = ob.reverseWithSpacesIntact(s)
		print(ans)


class Solution:

	def reverseWithSpacesIntact(self, s):
		revI = 0
		revS = s.replace(' ', '')[::-1]
		outS = ''
		lenS = len(s)
		for c in s:
			if c != ' ':
				outS += revS[revI]
				revI += 1
			else:
				outS += ' '
		return outS
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		ob = Solution()
		ans = ob.reverseWithSpacesIntact(s)
		print(ans)


class Solution:

	def reverseWithSpacesIntact(self, s):
		l = 0
		r = len(s) - 1
		ans = ''
		while l < len(s):
			if s[l] == ' ':
				ans += s[l]
				l += 1
				continue
			if s[r] == ' ':
				r -= 1
				continue
			ans += s[r]
			l += 1
			r -= 1
		return ans
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		ob = Solution()
		ans = ob.reverseWithSpacesIntact(s)
		print(ans)


class Solution:

	def reverseWithSpacesIntact(self, s):
		n = len(s)
		s = list(s)
		start = 0
		end = n - 1
		while start < end:
			if s[start] == ' ':
				start += 1
				continue
			elif s[end] == ' ':
				end -= 1
				continue
			else:
				(s[start], s[end]) = (s[end], s[start])
				start += 1
				end -= 1
		return ''.join(s)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		ob = Solution()
		ans = ob.reverseWithSpacesIntact(s)
		print(ans)


class Solution:

	def reverseWithSpacesIntact(self, s):
		(i, j) = (0, len(s) - 1)
		s = list(s)
		while i < j:
			if s[i] == ' ':
				i += 1
				continue
			elif s[j] == ' ':
				j -= 1
				continue
			elif i != ' ' and j != ' ':
				(s[i], s[j]) = (s[j], s[i])
				i += 1
				j -= 1
		return ''.join(s)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		ob = Solution()
		ans = ob.reverseWithSpacesIntact(s)
		print(ans)


class Solution:

	def reverseWithSpacesIntact(self, s):
		n = len(s)
		space_index = []
		result = ''
		for i in range(n):
			if s[i] == ' ':
				space_index.append(i)
		for ele in s:
			if ele != ' ':
				result = ele + result
		string = list(map(str, result))
		for i in space_index:
			string.insert(i, ' ')
		return ''.join(string)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		ob = Solution()
		ans = ob.reverseWithSpacesIntact(s)
		print(ans)


class Solution:

	def reverseWithSpacesIntact(self, s):
		spaces = []
		text = ''
		lst = list(s)
		for itr in range(len(lst)):
			if lst[itr] == ' ':
				spaces.append(itr)
			else:
				text += lst[itr]
		lst = list(text)
		lst.reverse()
		for itr in range(len(spaces)):
			lst.insert(spaces[itr], ' ')
		return ''.join(lst)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		ob = Solution()
		ans = ob.reverseWithSpacesIntact(s)
		print(ans)


class Solution:

	def reverseWithSpacesIntact(self, s):
		d = s.replace(' ', '')
		d = d[::-1]
		t = ''
		k = 0
		for i in s:
			if i != ' ':
				t += d[k]
				k += 1
			else:
				t += ' '
		return t
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		ob = Solution()
		ans = ob.reverseWithSpacesIntact(s)
		print(ans)


class Solution:

	def reverseWithSpacesIntact(self, s):
		n = list(s)
		op = []
		ind = []
		res = []
		ans = ''
		for i in range(len(n)):
			if n[i].isalpha() or n[i].isdigit():
				res.append(n[i])
			else:
				ind.append(i)
				op.append(n[i])
		res = res[::-1]
		for i in range(len(ind)):
			res.insert(ind[i], op[i])
		for i in res:
			ans += i
		return ans
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		ob = Solution()
		ans = ob.reverseWithSpacesIntact(s)
		print(ans)


class Solution:

	def reverseWithSpacesIntact(self, s):
		if ' ' in s:
			n_s = ''
			spaces = [x for x in range(len(s)) if s[x] == ' ']
			n_s = n_s.split()
			for i in range(len(s) - 1, -1, -1):
				if s[i] != ' ':
					n_s += s[i]
			for j in range(len(spaces)):
				n_s.insert(spaces[j], ' ')
			n_s = ''.join(n_s)
			ans = n_s
		else:
			ans = s[::-1]
		return ans
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		ob = Solution()
		ans = ob.reverseWithSpacesIntact(s)
		print(ans)


class Solution:

	def reverseWithSpacesIntact(self, s):
		low = 0
		high = len(s) - 1
		s = [str(ele) for ele in s]
		while low < high:
			if s[low] == ' ':
				low += 1
			elif s[high] == ' ':
				high -= 1
			elif (s[low] != ' ') & (s[high] != ' '):
				(s[low], s[high]) = (s[high], s[low])
				low += 1
				high -= 1
			else:
				pass
		return ''.join(s)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		ob = Solution()
		ans = ob.reverseWithSpacesIntact(s)
		print(ans)


class Solution:

	def reverseWithSpacesIntact(self, S):
		(l, e, s) = ([v for v in S], len(S) - 1, 0)
		while s < len(S) and e >= 0:
			if l[s] == ' ':
				s += 1
			elif S[e] == ' ':
				e -= 1
			else:
				l[s] = S[e]
				(s, e) = (s + 1, e - 1)
		return ''.join(l)


class Solution:

	def reverseWithSpacesIntact(self, s):
		S = list(s)
		n = len(S)
		(first, last) = (0, n - 1)
		while first < last:
			if S[first] == ' ':
				first += 1
			elif S[last] == ' ':
				last -= 1
			else:
				(S[first], S[last]) = (S[last], S[first])
				first += 1
				last -= 1
		s = ''
		for i in S:
			s += i
		return s


class Solution:

	def reverseWithSpacesIntact(self, s):
		S = ''
		st = ''
		for i in s:
			if i.isspace():
				pass
			else:
				st = st + i
		st = st[::-1]
		j = 0
		for i in s:
			if i.isspace():
				S = S + ' '
			else:
				S = S + st[j]
				j += 1
		return S


class Solution:

	def reverseWithSpacesIntact(self, s):
		a = s[::-1]
		a = a.replace(' ', '')
		b = ''
		j = 0
		for i in s:
			if i != ' ':
				b = b + a[j]
				j = j + 1
			else:
				b = b + ' '
		return b


class Solution:

	def reverseWithSpacesIntact(self, s):
		s = list(s)
		n = len(s)
		k = n - 1
		i = 0
		while i < k:
			if s[i] != ' ' and s[k] != ' ':
				t = s[i]
				s[i] = s[k]
				s[k] = t
				i += 1
				k -= 1
			elif s[i] == ' ':
				i += 1
			else:
				k -= 1
		return ''.join(s)