Given a string S, containing special characters and all the alphabets, reverse the string without
affecting the positions of the special characters.
Example 1:
Input: S = "A&B"
Output: "B&A"
Explanation: As we ignore '&' and
then reverse, so answer is "B&A".
Example 2:
Input: S = "A&x#
Output: "x&A#"
Explanation: we swap only A and x.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function reverse() which takes the string as inputs and returns required reverse string.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)
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 reverse(self, s):

        # code here

Solutions:
class Solution:

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


class Solution:

	def reverse(self, s):
		s = list(s)
		i = 0
		j = len(s) - 1
		while i < j:
			if s[i].isalpha() == False:
				i += 1
			if s[j].isalpha() == False:
				j -= 1
			if s[i].isalpha() and s[j].isalpha():
				val = s[i]
				s[i] = s[j]
				s[j] = val
				i += 1
				j -= 1
		return ''.join(s)


class Solution:

	def reverse(self, s):
		temp = ''
		for ch in s:
			if ch.isalpha():
				temp += ch
		rev = temp[::-1]
		ans = ''
		i = 0
		for ch in s:
			if ch.isalpha():
				ans += rev[i]
				i += 1
			else:
				ans += ch
		return ans


class Solution:

	def reverse(self, s):
		str = s
		str = list(str)
		temp = []
		for i in str:
			if i.isalpha():
				temp.append(i)
		temp = list(reversed(temp))
		i = 0
		j = 0
		while i < len(str):
			if str[i].isalpha():
				str[i] = temp[j]
				j += 1
			i += 1
		return ''.join(str)


class Solution:

	def reverse(self, s):
		arr = []
		for ele in s:
			arr.append(ele)
		b = []
		for i in range(len(arr)):
			if arr[i].isalpha() == True:
				b.append(arr[i])
				arr[i] = '0'
		c = b.reverse()
		k = 0
		for i in range(len(arr)):
			if arr[i] == '0':
				arr[i] = b[k]
				k = k + 1
		return ''.join(arr)


class Solution:

	def reverse(self, s):
		l = len(s)
		alphas = []
		nonAlphas = {}
		res = [None] * l
		for (i, c) in enumerate(s):
			if not c.isalpha():
				nonAlphas[i] = c
			else:
				alphas.append(c)
		for (i, c) in enumerate(res):
			if i in nonAlphas:
				res[i] = nonAlphas[i]
			else:
				res[i] = alphas.pop()
		return ''.join(res)


class Solution:

	def reverse(self, s):
		arr = [i for i in s]
		low = 0
		high = len(arr) - 1
		while low < high:
			if arr[low].isalpha() and arr[high].isalpha():
				(arr[low], arr[high]) = (arr[high], arr[low])
				low += 1
				high -= 1
			elif arr[low].isalpha() == False:
				low += 1
			elif arr[high].isalpha() == False:
				high -= 1
			else:
				low += 1
				high -= 1
		return ''.join(arr)


class Solution:

	def reverse(self, s):
		n = len(s)
		s1 = ''
		ans = ''
		special = "!@#$%^&*()-_+=';/,.?~`][{}|"
		for i in s:
			if i not in special:
				s1 += i
		s1 = s1[::-1]
		j = 0
		for i in range(n):
			if s[i] not in special:
				ans += s1[j]
				j += 1
			else:
				ans += s[i]
		return ans


def isAlpha(x):
	if ord(x) >= 97 and ord(x) <= 122:
		return True
	if ord(x) >= 65 and ord(x) <= 90:
		return True
	return False

class Solution:

	def reverse(self, s):
		(fp, lp) = (0, len(s) - 1)
		s = list(s)
		while fp < lp:
			if isAlpha(s[fp]) and isAlpha(s[lp]):
				(s[fp], s[lp]) = (s[lp], s[fp])
				fp += 1
				lp -= 1
			elif isAlpha(s[fp]):
				lp -= 1
			else:
				fp += 1
		return ''.join(s)


from collections import OrderedDict

class Solution:

	def reverse(self, s):
		string_lenght = len(s)
		alpha_s = ''
		dict_special_char = OrderedDict()
		for (element, index) in zip(s, range(string_lenght)):
			if element.isalpha():
				alpha_s = element + alpha_s
			else:
				dict_special_char[index] = element
		for (key, value) in dict_special_char.items():
			alpha_s = alpha_s[:key] + value + alpha_s[key:]
		return alpha_s


class Solution:

	def reverse(self, s):
		arr = list(s)
		res = ''
		for i in s:
			if i.isalpha():
				res += i
		res = res[::-1]
		count = 0
		for i in range(len(arr)):
			if arr[i].isalpha():
				arr[i] = res[count]
				count += 1
		res = ''.join(arr)
		return res


class Solution:

	def reverse(self, s):
		l = list(s)
		t = ''
		for i in s:
			if i.isalpha():
				t += i
		t = t[::-1]
		j = 0
		for i in range(len(l)):
			if l[i].isalpha() and j < len(t):
				l[i] = t[j]
				j += 1
		l = ''.join(l)
		return l


class Solution:

	def reverse(self, s):
		stack = []
		for i in range(len(s)):
			if s[i].isalpha():
				stack.append(s[i])
		s1 = ''
		for i in range(len(s)):
			if s[i].isalpha():
				s1 += stack.pop()
			else:
				s1 += s[i]
		return s1


class Solution:

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


class Solution:

	def reverse(self, s):
		h = ['.'] * len(s)
		str = ''
		for i in range(len(s)):
			if not s[i].isalpha():
				h[i] = s[i]
			else:
				str = str + s[i]
		out = ''
		j = len(str) - 1
		for i in range(len(h)):
			if h[i] != '.':
				out += h[i]
			else:
				out += str[j]
				j -= 1
		return out


class Solution:

	def reverse(self, s):
		v = []
		n = len(s)
		for i in s:
			if i.isalpha():
				v.append(i)
		v.reverse()
		j = 0
		s = list(s)
		for i in range(n):
			if s[i].isalpha():
				s[i] = v[j]
				j += 1
		return ''.join(s)


class Solution:

	def reverse(self, s):
		a = '[@_!#$%^&*()<>?/\\|}{~:`]'
		v = []
		n = len(s)
		for i in s:
			if i not in a:
				v.append(i)
		v.reverse()
		j = 0
		s = list(s)
		for i in range(n):
			if s[i] not in a:
				s[i] = v[j]
				j += 1
		return ''.join(s)


class Solution:

	def reverse(self, s):
		a = list(s)
		lis = ['^', '$', '&', '*', '@', '#', '%', '!', '(', ')', "'", ';', ':', '"', '/', '?', '>', '<', '-', '_', '=', '+', '[', ']', '{', '}', '`', '~']
		res = []
		for i in s:
			if i not in lis:
				res.append(i)
		res = res[::-1]
		j = 0
		for i in range(len(s)):
			if a[i] not in lis:
				a[i] = res[j]
				j += 1
		a = ''.join(a)
		return a


class Solution:

	def reverse(self, s):
		output = ''
		result = ''
		for i in s:
			if ord(i) >= 65 and ord(i) <= 90 or (ord(i) >= 97 and ord(i) <= 122):
				output = i + output
		i = 0
		count = 0
		while i < len(s):
			if ord(s[i]) >= 65 and ord(s[i]) <= 90 or (ord(s[i]) >= 97 and ord(s[i]) <= 122):
				result += output[count]
				count += 1
			else:
				result += s[i]
			i += 1
		return result


class Solution:

	def reverse(self, s):
		s = [i for i in s]
		s1 = ''
		for i in s:
			if i.isalpha():
				s1 += i
		s1 = s1[::-1]
		j = 0
		for i in range(len(s)):
			if s[i].isalpha():
				s[i] = s1[j]
				j += 1
		return ''.join(s)


class Solution:

	def reverse(self, s):
		arr = []
		st = []
		for (index, value) in enumerate(s):
			if value.isalpha():
				st.append(value)
			else:
				arr.append(index)
		st = st[::-1]
		for v in arr:
			st.insert(v, s[v])
		return ''.join(st)


class Solution:

	def reverse(self, s):
		alphabet = [chr(i) for i in range(ord('a'), ord('z') + 1)] + [chr(i) for i in range(ord('A'), ord('Z') + 1)]
		temp = []
		new = ''
		for i in s:
			if i in alphabet:
				temp.append(i)
		temp.reverse()
		i = 0
		for j in range(len(s)):
			if s[j] in alphabet:
				new += temp[i]
				i += 1
				continue
			new += s[j]
		return new


class Solution:

	def reverse(self, s):
		pivot_left = 0
		pivot_right = len(s) - 1
		while pivot_left < pivot_right:
			if s[pivot_left].isalpha() and s[pivot_right].isalpha():
				(c_from_left, c_from_right) = (s[pivot_left], s[pivot_right])
				s = s[:pivot_left] + c_from_right + s[pivot_left + 1:pivot_right] + c_from_left + s[pivot_right + 1:]
				pivot_left += 1
				pivot_right -= 1
			if s[pivot_left].isalpha() == False:
				pivot_left += 1
			if s[pivot_right].isalpha() == False:
				pivot_right -= 1
		return s


class Solution:

	def reverse(self, s):
		s = list(s)
		stack = []
		for i in s:
			if i.isalpha():
				stack.append(i)
		for i in range(len(s)):
			if s[i].isalpha():
				s[i] = stack.pop()
		return ''.join(s)


import copy

class Solution:

	def reverse(self, s):
		(res, n) = (list(copy.deepcopy(s)), len(s) - 1)
		for i in range(len(s)):
			if s[i].isalpha():
				while True:
					if s[n].isalpha():
						res[n] = s[i]
						n += -1
						break
					n += -1
		return ''.join(res)


class Solution:

	def reverse(self, s):
		lst = []
		ans = ''
		for i in range(0, len(s)):
			if s[i].isalpha() == True:
				lst.append(s[i])
			else:
				continue
		lg = len(lst)
		for j in range(0, len(s)):
			if s[j].isalpha() == True:
				ans += lst[lg - 1]
				lg -= 1
			else:
				ans += s[j]
		return ans


class Solution:

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


class Solution:

	def reverse(self, s):
		l = list(s)
		start = 0
		end = len(s) - 1
		while start < end:
			if l[start].isalpha() and l[end].isalpha():
				(l[start], l[end]) = (l[end], l[start])
				start += 1
				end -= 1
			elif l[start].isalpha() == False:
				start += 1
			elif l[end].isalpha() == False:
				end -= 1
		return ''.join(l)


class Solution:

	def reverse(self, s):
		i = 0
		j = len(s) - 1
		chars = [c for c in s]
		while i < j:
			while not s[i].isalpha() and i < j:
				i += 1
			while not s[j].isalpha() and j > i:
				j -= 1
			if i >= j:
				break
			chars[j] = s[i]
			chars[i] = s[j]
			i += 1
			j -= 1
		return ''.join(chars)


def isAlphabet(char):
	if char >= 'a' and char <= 'z' or (char >= 'A' and char <= 'Z'):
		return True
	return False

class Solution:

	def reverse(self, s):
		start = 0
		end = len(s) - 1
		list_string = list(s)
		while start < end:
			element1 = list_string[start]
			element2 = list_string[end]
			isElement1Alphabet = isAlphabet(element1)
			isElement2Alphabet = isAlphabet(element2)
			if isElement1Alphabet and isElement2Alphabet:
				temp = element1
				list_string[start] = element2
				list_string[end] = temp
				start += 1
				end -= 1
			if not isElement1Alphabet:
				start += 1
			if not isElement2Alphabet:
				end -= 1
		return ''.join(list_string)


class Solution:

	def reverse(self, s):
		(start, end) = (0, len(s) - 1)
		sL = list(s)
		while start < end:
			if not s[start].isalpha():
				start += 1
			elif not s[end].isalpha():
				end -= 1
			else:
				(sL[start], sL[end]) = (sL[end], sL[start])
				start += 1
				end -= 1
		return ''.join(sL)


class Solution:

	def reverse(self, s):
		a = []
		for i in s:
			a.append(i)
		(i, j) = (0, len(s) - 1)
		while i < j:
			if a[i].isalpha() and a[j].isalpha():
				(a[i], a[j]) = (a[j], a[i])
				i += 1
				j -= 1
			elif a[i].isalpha() and a[j].isalpha() == False:
				j -= 1
			else:
				i += 1
		op = ''.join((str(x) for x in a))
		return op


class Solution:

	def reverse(self, s):
		indx = list()
		alpha = list()
		for (index, value) in enumerate(s):
			if value.isalpha():
				alpha.append(value)
			else:
				indx.append(index)
		alpha = alpha[::-1]
		for i in indx:
			alpha.insert(i, s[i])
		return ''.join(alpha)


class Solution:

	def reverse(self, s):
		strl = []
		for i in s:
			if i.isalpha():
				strl.append(i)
		out = ''
		for i in s:
			if i.isalpha():
				out += strl[-1]
				strl.pop(-1)
			else:
				out += i
		return out


class Solution:

	def reverse(self, s):
		low = 0
		high = len(s) - 1
		a = list(s)
		while low < high:
			if a[low].isalpha() and a[high].isalpha():
				temp = a[low]
				a[low] = a[high]
				a[high] = temp
				low = low + 1
				high = high - 1
			elif not a[low].isalpha():
				low = low + 1
			else:
				high = high - 1
		return ''.join(a)


class Solution:

	def reverse(self, s):
		i = 0
		j = len(s) - 1
		s = list(s)
		while i < j:
			if (s[i] >= 'a' and s[i] <= 'z' or (s[i] >= 'A' and s[i] <= 'Z')) and (s[j] >= 'a' and s[j] <= 'z' or (s[j] >= 'A' and s[j] <= 'Z')):
				(s[i], s[j]) = (s[j], s[i])
				i += 1
				j -= 1
			elif s[i] >= 'a' and s[i] <= 'z' or (s[i] >= 'A' and s[i] <= 'Z'):
				j -= 1
			elif s[j] >= 'a' and s[j] <= 'z' or (s[j] >= 'A' and s[j] <= 'Z'):
				i += 1
			else:
				i += 1
				j -= 1
		st = ''
		st = st.join(s)
		return st


class Solution:

	def reverse(self, s):
		li = []
		for i in range(len(s)):
			li.append(s[i])
		d1 = {}
		for i in s:
			if i.isupper():
				d1[i] = d1.get(i, 0) + 1
			elif i.islower():
				d1[i] = d1.get(i, 0) + 1
		(i, j) = (0, len(li) - 1)
		while i < j:
			if li[j] in d1 and li[i] in d1:
				(li[i], li[j]) = (li[j], li[i])
				i += 1
				j -= 1
			elif li[j] not in d1:
				j -= 1
			elif li[i] not in d1:
				i += 1
		string1 = ''
		for i in range(len(li)):
			string1 += li[i]
		return string1


class Solution:

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

	def isAlphabet(self, c):
		return 'a' <= c and c <= 'z' or ('A' <= c and c <= 'Z')


class Solution:

	def reverse(self, s):
		n = len(s)
		res = list(s)
		c = ''
		for i in s:
			if i.isalpha():
				c += i
		c = c[::-1]
		j = 0
		for i in range(n):
			if res[i].isalpha():
				res[i] = c[j]
				j += 1
		return ''.join(res)


class Solution:

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


class Solution:

	def reverse(self, s):
		let = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
		li = list(s)
		i = 0
		j = len(s) - 1
		while i <= j:
			if s[i] in let and s[j] in let:
				(li[i], li[j]) = (li[j], li[i])
				i += 1
				j -= 1
			elif s[i] in let and s[j] not in let:
				j -= 1
			elif s[i] not in let and s[j] in let:
				i += 1
			elif s[i] not in let and s[j] not in let:
				i += 1
				j -= 1
		return ''.join(li)


class Solution:

	def reverse(self, s):
		s = list(s)
		l = 0
		r = len(s) - 1
		while l < r:
			if s[l].isalpha() and s[r].isalpha():
				(s[l], s[r]) = (s[r], s[l])
				l += 1
				r -= 1
			if not s[l].isalnum() and (not s[l].isspace()):
				l += 1
			if not s[r].isalnum() and (not s[r].isspace()):
				r -= 1
		return ''.join(s)


class Solution:

	def reverse(self, s):
		s = list(s)
		a = []
		for x in s[::-1]:
			if x.isalpha():
				a.append(x)
		k = 0
		for i in range(len(s)):
			if s[i].isalpha():
				s[i] = a[k]
				k += 1
		return ''.join(s)


class Solution:

	def reverse(self, s):

		def findNextLetterPos(temp, start, isFront):
			pos = 0
			if isFront:
				for x in range(start, len(temp)):
					if temp[x] >= 'a' and temp[x] <= 'z' or (temp[x] >= 'A' and temp[x] <= 'Z'):
						pos = x
						break
					else:
						continue
			else:
				for x in range(start, -1, -1):
					if temp[x] >= 'a' and temp[x] <= 'z' or (temp[x] >= 'A' and temp[x] <= 'Z'):
						pos = x
						break
					else:
						continue
			return pos
		temp = []
		for x in range(len(s)):
			temp.append(s[x])
		front = findNextLetterPos(temp, 0, True)
		back = findNextLetterPos(temp, len(temp) - 1, False)
		while front < back:
			(temp[front], temp[back]) = (temp[back], temp[front])
			front = findNextLetterPos(temp, front + 1, True)
			back = findNextLetterPos(temp, back - 1, False)
		result = ''
		return result.join(temp)


class Solution:

	def rev(self, s, start, end):
		l = start
		r = end - 1
		while l < r:
			tmp = s[l]
			s[l] = s[r]
			s[r] = tmp
			l += 1
			r -= 1

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


class Solution:

	def rev(self, s, start, end):
		l = start
		r = end - 1
		while l < r:
			tmp = s[l]
			s[l] = s[r]
			s[r] = tmp
			l += 1
			r -= 1

	def reverse(self, s):
		newstr = [''] * len(s)
		x = 0
		for i in range(len(s)):
			if s[i].isalpha():
				newstr[x] = s[i]
				x += 1
		self.rev(newstr, 0, x)
		i = 0
		j = 0
		sl = list(s)
		while i < len(sl):
			if sl[i].isalpha():
				sl[i] = newstr[j]
				j += 1
			i += 1
		return ''.join(sl)


class Solution:

	def reverse(self, s):
		arr = []
		for i in s:
			arr.append(i)
		special = '+-*/%!~=^_()[]{}`@#$&'
		one = 0
		two = len(s) - 1
		while one < two:
			if arr[one] not in special and arr[two] not in special:
				(arr[one], arr[two]) = (arr[two], arr[one])
				one += 1
				two -= 1
			elif arr[one] in special:
				one += 1
			else:
				two -= 1
		return ''.join(arr)


class Solution:

	def reverse(self, s):
		s = list(s)
		lenS = len(s)
		leftIndex = 0
		rightIndex = lenS - 1
		while leftIndex < rightIndex:
			if s[leftIndex].isalpha():
				while True:
					if s[rightIndex].isalpha() is False:
						rightIndex -= 1
					else:
						break
				(s[leftIndex], s[rightIndex]) = (s[rightIndex], s[leftIndex])
				rightIndex -= 1
			leftIndex += 1
		return ''.join(s)


class Solution:

	def reverse(self, s):
		special_chars = "/^[!@#$%^&*()_+\\-=\\[\\]{};':\\|,.<>\\/?]*$/"
		l = 0
		r = len(s) - 1
		reversed_str = ''
		while l != len(s):
			if s[l].isalpha() == False:
				reversed_str += s[l]
			else:
				while s[r].isalpha() == False:
					r -= 1
				reversed_str += s[r]
				r -= 1
			l += 1
		return reversed_str


class Solution:

	def reverse(self, s):
		tempChars = []
		newString = ''
		for char in s:
			if char.isalpha():
				tempChars.append(char)
		index = len(tempChars) - 1
		for char in s:
			if char.isalpha():
				newString += tempChars[index]
				index -= 1
			else:
				newString += char
		return newString


class Solution:

	def reverse(self, s):
		symb = ''
		ch = ''
		j = 0
		l = set()
		for i in s:
			if i.isalpha():
				ch += i
			else:
				symb += i
				l.add(j)
			j += 1
		res = ''
		ch = ch[::-1]
		si = 0
		m = 0
		for i in range(len(s)):
			if i in l:
				res += symb[si]
				si += 1
			else:
				res += ch[m]
				m += 1
		return res


class Solution:

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


class Solution:

	def reverse(self, s):
		a = list(s)
		st = ''
		for i in s:
			if i.isalpha():
				st += i
		b = st[::-1]
		c = 0
		for i in range(len(a)):
			if a[i].isalpha():
				a[i] = b[c]
				c += 1
		return ''.join(a)


class Solution:

	def reverse(self, s):
		h = list(s)
		t = ''
		for i in s:
			if i.isalpha():
				t += i
		l = t[::-1]
		c = 0
		for i in range(len(h)):
			if h[i].isalpha():
				h[i] = l[c]
				c += 1
		r = ''.join(h)
		return r


class Solution:

	def reverse(self, s):
		n = len(s)
		temp = ''
		for i in range(n):
			if s[i].isalpha():
				temp = temp + s[i]
		arr = list(s)
		rev = temp[::-1]
		k = 0
		for i in range(len(s)):
			if arr[i].isalpha():
				arr[i] = rev[k]
				k += 1
		final = ''
		for i in arr:
			final += i
		return final


class Solution:

	def reverse(self, s):
		s = list(s)
		end = len(s) - 1
		start = 0
		while start < end:
			if s[start].isalnum() and s[end].isalnum():
				(s[start], s[end]) = (s[end], s[start])
				start += 1
				end -= 1
			elif not s[start].isalnum():
				start += 1
			else:
				end -= 1
		return ''.join(s)


class Solution:

	def reverse(self, s):
		res = ['' for _ in range(len(s))]
		(i, j) = (0, len(s) - 1)
		while i <= j:
			if not s[i].isalpha():
				res[i] = s[i]
				i += 1
			if not s[j].isalpha():
				res[j] = s[j]
				j -= 1
			if s[i].isalpha() and s[j].isalpha():
				res[i] = s[j]
				res[j] = s[i]
				i += 1
				j -= 1
		return ''.join(res)


class Solution:

	def reverse(self, s):
		n = len(s) - 1
		k = ''
		m = ''
		sp = '["@_!#$%^&*()<>?/\\|}`{~:]'
		i = 0
		for i in range(len(s)):
			if s[i] not in sp:
				k += s[i]
		k = k[::-1]
		i = 0
		for j in range(len(s)):
			if s[j] not in sp:
				m += k[i]
				i += 1
			else:
				m += s[j]
		return m


class Solution:

	def reverse(self, s):
		s = list(s)
		i = 0
		j = len(s) - 1
		ans = ''
		while i < j:
			if s[i].isalpha() != True:
				i += 1
				continue
			elif s[j].isalpha() != True:
				j -= 1
				continue
			else:
				(s[i], s[j]) = (s[j], s[i])
				i += 1
				j -= 1
		for i in s:
			ans += i
		return ans


class Solution:

	def reverse(self, s):
		s = list(s)
		st = 0
		end = len(s) - 1
		while st < end:
			if s[st].isalpha() == True and s[end].isalpha() == True:
				(s[st], s[end]) = (s[end], s[st])
				st += 1
				end -= 1
			if s[st].isalpha() != True:
				st += 1
			if s[end].isalpha() != True:
				end -= 1
		return ''.join(s)


class Solution:

	def reverse(self, s):
		tmp = []
		for i in s:
			if i.isalpha():
				tmp.append(i)
		s = list(s)
		for (idx, i) in enumerate(s):
			if i.isalpha():
				s[idx] = tmp.pop()
		return ''.join(s)


class Solution:

	def reverse(self, s):
		sp = []
		pos = []
		l = []
		for i in range(len(s)):
			if s[i].isalpha():
				l.append(s[i])
			else:
				sp.append(s[i])
				pos.append(i)
		l = l[::-1]
		for i in range(len(sp)):
			l.insert(pos[i], sp[i])
		S = ''
		for i in l:
			S = S + i
		return S


class Solution:

	def reverse(self, s):
		temp = [''] * len(s)
		x = 0
		for i in range(len(s)):
			if s[i] >= 'a' and s[i] <= 'z' or (s[i] >= 'A' and s[i] <= 'Z'):
				temp[x] = s[i]
				x += 1
		i = 0
		j = x - 1
		while i < j:
			(temp[i], temp[j]) = (temp[j], temp[i])
			i += 1
			j -= 1
		lst = list(s)
		x = 0
		for i in range(len(s)):
			if s[i] >= 'a' and s[i] <= 'z' or (s[i] >= 'A' and s[i] <= 'Z'):
				lst[i] = temp[x]
				x += 1
		revstr = ''
		for i in range(len(s)):
			revstr += lst[i]
		return revstr


class Solution:

	def reverse(self, s):
		lst = []
		for word in s.split(' '):
			letters = [c for c in word if c.isalpha()]
			for c in word:
				if c.isalpha():
					lst.append(letters.pop())
					continue
				else:
					lst.append(c)
			lst.append(' ')
		return ''.join(lst)


class Solution:

	def reverse(self, s):
		L = []
		res = ''
		for i in s:
			L.append(i)
		i = 0
		j = len(L) - 1
		while i < j:
			if L[i].isalpha():
				if L[j].isalpha():
					(L[i], L[j]) = (L[j], L[i])
					i += 1
					j -= 1
				else:
					j -= 1
			else:
				i += 1
		for w in L:
			res += w
		return res


class Solution:

	def reverse(self, s):
		i = 0
		j = len(s) - 1
		ls = list(s)
		while i < j:
			while i < j and (not ls[i].isalpha()):
				i += 1
			while i < j and (not ls[j].isalpha()):
				j -= 1
			temp = ls[i]
			ls[i] = ls[j]
			ls[j] = temp
			i += 1
			j -= 1
		return ''.join(ls)


class Solution:

	def reverse(self, s):
		st = ''
		arr = []
		brr = []
		hm = {}
		for i in range(len(s)):
			if ord(s[i]) >= 65 and ord(s[i]) <= 90 or (ord(s[i]) >= 97 and ord(s[i]) <= 122):
				hm[s[i]] = 1
				arr.append(s[i])
			else:
				brr.append(s[i])
		for i in range(len(s)):
			if hm.get(s[i]):
				t = arr.pop()
			else:
				t = brr.pop(0)
			st += t
		return st


class Solution:

	def reverse(self, s):
		arr = list(s)
		n = len(arr)
		low = 0
		high = n - 1
		while low < high:
			while low < high and (not arr[low].isalpha()):
				low += 1
			while high > low and (not arr[high].isalpha()):
				high -= 1
			(arr[low], arr[high]) = (arr[high], arr[low])
			low += 1
			high -= 1
		return ''.join(arr)


class Solution:

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


class Solution:

	def reverse(self, s):
		n = len(s)
		special_char_set = {'#', '@', '!', '&', '*', '(', ')', '_', '-', '+', '=', '[', ']', '{', '}', '|', ';', ':', '"', '<', '>', '?', '/', '~', '`', '^', '%', '$', '\\', ','}
		special_char_idx = []
		for (idx, char) in enumerate(s):
			if char in special_char_set:
				special_char_idx.append([char, idx])
		temp_s = ''
		for i in s:
			if i not in special_char_set:
				temp_s += i

		def reverse_str(temp_s):
			tols = list(temp_s)
			new_s = ''
			while tols:
				popped = tols.pop()
				new_s += popped
			new_s = list(new_s)
			return new_s
		rev = reverse_str(temp_s)
		for i in special_char_idx:
			rev.insert(i[1], i[0])
		ans = ''.join(rev)
		return ans


import string

class Solution:

	def reverse(self, s):
		alp = string.ascii_lowercase + string.ascii_uppercase
		ch = [i for i in s if i in alp]
		res = ''
		idx = len(ch) - 1
		for i in s:
			if i in alp:
				res += ch[idx]
				idx -= 1
			else:
				res += i
		return res


class Solution:

	def reverse(self, s):
		l = []
		t = ''
		for i in s:
			l.append(i)
			if i.isalpha():
				t += i
		t = t[::-1]
		k = 0
		for i in range(len(l)):
			if l[i].isalpha():
				l[i] = t[k]
				k += 1
		return ''.join(l)


class Solution:

	def reverse(self, s):
		s = list(s)
		start = 0
		end = len(s) - 1
		alphabets = set((x for x in 'abcdefghijklmnopqrstuvwxyz'))
		while start < end:
			while s[start].lower() not in alphabets and start < end:
				start += 1
			while s[end].lower() not in alphabets and start < end:
				end -= 1
			if start < end:
				(s[start], s[end]) = (s[end], s[start])
				start += 1
				end -= 1
		return ''.join(s)


class Solution:

	def reverse(self, s):
		l = len(s) - 1
		r = list(s)
		f = 0
		while f < l:
			b = s[f]
			if not b.isalpha():
				f += 1
				continue
			e = s[l]
			if not e.isalpha():
				l -= 1
				continue
			r[l] = b
			r[f] = e
			f += 1
			l -= 1
		return ''.join(r)


class Solution:

	def reverse(self, s):
		n = len(s)
		l = list(s)
		a = ''
		for i in s:
			if i.isalpha():
				a = a + i
		S = a[::-1]
		j = 0
		for i in range(n):
			if l[i].isalpha():
				l[i] = S[j]
				j += 1
		l = ''.join(l)
		return l