Given a String S, reverse the string without reversing its individual words. Words are separated by dots.
Example 1:
Input:
S = i.like.this.program.very.much
Output: much.very.program.this.like.i
Explanation: After reversing the whole
string(not individual words), the input
string becomes
much.very.program.this.like.i
Example 2:
Input:
S = pqr.mno
Output: mno.pqr
Explanation: After reversing the whole
string , the input string becomes
mno.pqr
Your Task:
You dont need to read input or print anything. Complete the function reverseWords() which takes string S as input parameter and returns a string containing the words in reversed order. Each word in the returning string should also be separated by '.' 
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)
Constraints:
1 <= |S| <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: # User function Template for python3



class Solution:

    

    #Function to reverse words in a given string.

    def reverseWords(self,S):

        # code here 

Solutions:
class Solution:

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


class Solution:

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


class Solution:

	def reverseWords(self, S):
		sp_S = S.split('.')
		rev_S = sp_S[::-1]
		return '.'.join(rev_S)


class Solution:

	def reverseWords(self, S):
		s = S.split('.')
		s.reverse()
		return '.'.join(s)


class Solution:

	def reverseWords(self, S):
		string = S.split('.')
		reverse = string[::-1]
		reverses = '.'.join(reverse)
		return reverses


class Solution:

	def reverseWords(self, S):
		r = S.split('.')
		w = '.'.join(r[::-1])
		return w


class Solution:

	def reverseWords(self, S):
		rev = S.split('.')[::-1]
		res = '.'.join(rev)
		return res


class Solution:

	def reverseWords(self, S):
		my_list = S.split('.')
		my_list.reverse()
		return '.'.join(my_list)


class Solution:

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


class Solution:

	def reverseWords(self, S):
		p = ''
		for i in S.split('.')[::-1]:
			p = p + i
			p = p + '.'
		return p[:-1]


class Solution:

	def reverseWords(self, S):
		s = ''
		l = S.split('.')
		l.reverse()
		for i in l:
			s = s + i + '.'
		return s[0:-1]


class Solution:

	def reverseWords(self, S):
		stack = []
		st = ''
		for i in S:
			if i == '.':
				stack.append(st)
				st = ''
			else:
				st += i
		stack.append(st)
		ans = ''
		for i in range(len(stack)):
			ans += stack.pop()
			ans += '.'
		return ans[:-1]


class Solution:

	def reverseWords(self, S):
		return '.'.join(reversed(S.split('.')))


class Solution:

	def reverseWords(self, s):
		s = list(s.split('.'))
		s.reverse()
		return '.'.join(s)


class Solution:

	def reverseWords(self, S):
		wrd = S.split('.')
		revers_wrd = wrd[::-1]
		new_str = '.'.join(revers_wrd)
		return new_str


class Solution:

	def reverseWords(self, S):
		result = ''
		for ele in S.split('.'):
			result = ele + '.' + result
		return result[0:-1]


class Solution:

	def reverseWords(self, S):
		S_list = S.split('.')
		new_S = ''
		s_len = len(S_list)
		for i in range(s_len):
			new_S = new_S + S_list[s_len - i - 1] + '.'
		return new_S[:-1]


class Solution:

	def reverseWords(self, S):
		a = [i for i in S.split('.')]
		k = a[::-1]
		s = k[0]
		for q in range(1, len(k)):
			s += '.'
			s += k[q]
		return s


class Solution:

	def reverseWords(self, S):
		k = S.split('.')
		u = ''
		for i in range(len(k) - 1, -1, -1):
			u += k[i]
			if i != 0:
				u += '.'
		return u


class Solution:

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


class Solution:

	def reverseWords(self, S):
		l = S.split('.')
		z = l[::-1]
		str1 = '.'.join(z)
		return str1


class Solution:

	def reverse(self, s, start, end):
		while start < end:
			(s[start], s[end]) = (s[end], s[start])
			start += 1
			end -= 1

	def reverseWords(self, S):
		s = list(S)
		n = len(s)
		ind = []
		for i in range(n):
			if s[i] == '.':
				ind.append(i)
		start = 0
		for i in ind:
			end = i - 1
			self.reverse(s, start, end)
			start = i + 1
		self.reverse(s, start, len(s) - 1)
		st = ''
		for i in s:
			st = i + st
		return st


class Solution:

	def reverseWords(self, s):
		l = list(s.split('.'))
		i = len(l) - 1
		s = ''
		while i > 0:
			s += l[i] + '.'
			i = i - 1
		s += l[0]
		return s


class Solution:

	def reverseWords(self, S):
		ret = ''
		lst = S.split('.')
		for i in range(len(lst) - 1, -1, -1):
			ret += lst[i]
			if i != 0:
				ret += '.'
		return ret


class Solution:

	def reverseWords(self, s):
		k = s.replace('.', ' ')
		m = k.split()
		result = m[::-1]
		t = ' '.join(result)
		final = t.replace(' ', '.')
		return final


class Solution:

	def reverseWords(self, S):
		words = S.split('.')
		rev = '.'.join(reversed(words))
		return rev


class Solution:

	def reverseWords(self, S):
		x = S.split('.')
		str = ''
		for i in range(len(x) - 1, -1, -1):
			str += x[i]
			if i != 0:
				str += '.'
		return str


class Solution:

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


class Solution:

	def reverseWords(self, S):
		x = S.split('.')
		y = []
		for i in range(1, len(x) + 1):
			y.append(x[-i])
		k = '.'.join(y)
		return k


class Solution:

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


class Solution:

	def reverseWords(self, S):
		ans = []
		k = ''
		for i in range(len(S)):
			if S[i] == '.':
				ans.append(k)
				k = ''
			else:
				k += S[i]
			if i == len(S) - 1:
				ans.append(k)
		ans.reverse()
		return '.'.join(ans)


class Solution:

	def reverseWords(self, S):
		if len(S) == 0:
			return S
		ans = ''
		word = ''
		for ele in S:
			if ele == '.':
				ans = word + '.' + ans
				word = ''
			else:
				word += ele
		if word != '':
			ans = word + '.' + ans
		return ans[:-1]


class Solution:

	def reverseWords(self, S):
		result = ''
		words = []
		curr_word = ''
		for i in range(len(s)):
			if s[i] == '.':
				words.append(curr_word)
				curr_word = ''
			else:
				curr_word += s[i]
		words.append(curr_word)
		for i in range(len(words) - 1, -1, -1):
			result += words[i]
			if i:
				result += '.'
		return result


class Solution:

	def reverseWords(self, S):
		li = list(s.split('.'))
		li.reverse()
		pi = '.'.join(li)
		return pi


class Solution:

	def reverseWords(self, S):
		words = S.split('.')[::-1]
		reversed_string = '.'.join(words)
		return reversed_string


class Solution:

	def reverseWords(self, S):
		l = S.split('.')
		kk = len(l)
		aa = ''
		for k in l:
			kk = kk - 1
			if kk != 0:
				cc = '.'
			else:
				cc = ''
			aa += l[kk] + cc
		return aa


class Solution:

	def reverseWords(self, S):
		words = S.split('.')
		words.reverse()
		ans = ''
		for i in range(len(words) - 1):
			ans = ans + words[i] + '.'
		ans = ans + words[len(words) - 1]
		return ans


class Solution:

	def reverseWords(self, S):
		list_1 = s.split('.')
		l = len(list_1)
		m = ''
		for i in range(l - 1, -1, -1):
			m += list_1[i] + '.'
		return m[:-1]


class Solution:

	def reverseWords(self, S):
		l = S.split('.')
		l.reverse()
		ans = '.'.join(l)
		return ans


class Solution:

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


class Solution:

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


class Solution:

	def reverseWords(self, S):
		(x, l) = ('', [])
		for i in S:
			if i == '.':
				l.append(x)
				x = ''
			else:
				x = x + i
		l.append(x)
		x = ''
		for i in l[::-1]:
			x = x + i + '.'
		return x[:len(x) - 1]


class Solution:

	def reverseWords(self, S):
		b = S.split('.')
		b.reverse()
		c = '.'.join(b)
		return c


class Solution:

	def reverseWords(self, s):
		return '.'.join(list(reversed(s.split('.'))))


class Solution:

	def reverseWords(self, S):
		st = S.split('.')
		st1 = st[::-1]
		result = '.'.join(st1)
		return result


class Solution:

	def reverseWords(self, S):
		words = S.split('.')
		words.reverse()
		reversed_s = '.'.join(words)
		return reversed_s


class Solution:

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


class Solution:

	def reverseWords(self, S):
		s = S.split('.')
		string = ''
		for i in range(len(s) - 1, -1, -1):
			string = string + s[i] + '.'
		return string[:-1]


class Solution:

	def reverse(self, S, start, end):
		end = end - 1
		while start < end:
			(S[start], S[end]) = (S[end], S[start])
			start += 1
			end -= 1

	def reverseWords(self, S):
		S = list(S)
		start = 0
		for (end, char) in enumerate(S):
			if char == '.':
				self.reverse(S, start, end)
				start = end + 1
		self.reverse(S, start, len(S))
		self.reverse(S, 0, len(S))
		return ''.join(S)


class Solution:

	def reverseWords(self, S):
		temp = S.split('.')
		temp.reverse()
		ans = '.'.join(temp)
		return ans


class Solution:

	def reverseWords(self, S):
		res = ''
		last_index_of_current_word = len(S)
		for i in range(len(S) - 1, -1, -1):
			if S[i] == '.':
				res += S[i + 1:last_index_of_current_word] + '.'
				last_index_of_current_word = i
		res += S[0:last_index_of_current_word]
		return res


class Solution:

	def reverseWords(self, S):
		temp = S.split('.')
		temp.reverse()
		ret = '.'.join(temp)
		return ret


class Solution:

	def reverseWords(self, S):
		ret = []
		s = ''
		S += '.'
		for i in S:
			if i == '.':
				ret.append(s)
				s = ''
			else:
				s += i
		res = ''
		for i in range(len(ret) - 1, -1, -1):
			res += ret[i]
			res += '.'
		return res[:len(S) - 1]


class Solution:

	def reverseWords(self, S):
		s = S.replace('.', ' ')
		s1 = s.split()
		rev_words = []
		start = 0
		end = len(s1) - 1
		while start < end:
			temp = s1[start]
			s1[start] = s1[end]
			s1[end] = temp
			start += 1
			end -= 1
		return '.'.join(s1)


class Solution:

	def reverseWords(self, S):
		words = S.split('.')
		reverse_sentence = '.'.join(reversed(words))
		return reverse_sentence


class Solution:

	def reverseWords(self, S):
		s = S.split('.')
		rev = str()
		length = len(s)
		for i in reversed(range(length)):
			rev = rev + str(s[i])
			rev = rev + '.'
		rev = rev.rstrip(rev[-1])
		return rev


class Solution:

	def reverseWords(self, S):
		S = S.split('.')
		for i in range(len(S) // 2):
			(S[i], S[len(S) - i - 1]) = (S[len(S) - i - 1], S[i])
		return '.'.join(S)


class Solution:

	def reverseWords(self, S):
		l = S.split('.')
		l.reverse()
		m = '.'.join(l)
		return m


class Solution:

	def reverseWords(self, s):
		l = s.split('.')
		s1 = ''
		for i in range(len(l) - 1, -1, -1):
			if i != 0:
				s1 = s1 + l[i] + '.'
			else:
				s1 = s1 + l[i]
		return s1


class Solution:

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


class Solution:

	def reverseWords(self, S):
		t = S.split('.')
		m = '.'.join(reversed(t))
		return m


class Solution:

	def reverseWords(self, S):
		s = ''
		res = ''
		for i in range(0, len(S)):
			if S[i] == '.':
				s = res + s
				s = S[i] + s
				res = ''
			else:
				res += S[i]
		s = res + s
		return s


class Solution:

	def reverseWords(self, S):
		lis = S.split('.')
		lis = reversed(lis)
		lis = [x for x in lis if x != '.']
		return '.'.join(lis)