You are given a string S, convert it into a magical string.
A string can be made into a magical string if the alphabets are swapped in the given manner: a->z or z->a, b->y or y->b, and so on.  
 
Note: All the alphabets in the string are in lowercase.
 
Example 1:
Input:
S = varun
Output:
ezifm
Explanation:
Magical string of "varun" 
will be "ezifm" 
since v->e , a->z , 
r->i , u->f and n->m.
 
Example 2:
Input:
S = akshay
Output:
zphszb
Explanation:
Magical string of "akshay" 
will be "zphszb" 
since a->z , k->p , s->h , 
h->s , a->z and y->b.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function magicalString() which takes the string S and returns the magical string.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
 
Constraints:
1<=Length of String<=100

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:

    def magicalString (ob,S):

        # code here 

Solutions:
class Solution:

	def magicalString(ob, S):
		s = ''
		for i in range(len(S)):
			s += chr(122 - (ord(S[i]) - ord('a')))
		return s


class Solution:

	def magicalString(ob, S):
		S = list(S)
		for i in range(len(S)):
			S[i] = chr(abs(ord(S[i]) - ord('a') - ord('z')))
		return ''.join(S)


class Solution:

	def magicalString(ob, S):
		l1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
		t = ''
		for i in S:
			postion = l1.index(i)
			new_position = 25 - postion
			t += l1[new_position]
		return t


class Solution:

	def magicalString(ob, S):
		r = ''
		for char in S:
			dist_from_a = ord(char) - ord('a')
			swapped_char = chr(ord('z') - dist_from_a)
			r += swapped_char
		return r


class Solution:

	def magicalString(ob, S):
		k = ''
		for i in S:
			ordd = ord(i)
			diff = 122 - ordd
			k += chr(97 + diff)
		return k


class Solution:

	def magicalString(ob, S):
		s = ''
		for i in S:
			s += chr(97 + 122 - ord(i))
		return s


class Solution:

	def magicalString(ob, S):
		alpha = 'abcdefghijklmnopqrstuvwxyz'
		rev = alpha[::-1]
		d = dict(zip(alpha, rev))
		return ''.join((d[i] for i in S))


class Solution:

	def magicalString(ob, S):
		t = 'abcdefghijklmnopqrstuvwxyz'
		r = t[::-1]
		b = ''
		for i in S:
			b += r[t.index(i)]
		return b


class Solution:

	def magicalString(ob, S):
		mapping = {chr(i): chr(219 - i) for i in range(ord('a'), ord('z') + 1)}
		reversed_s = ''.join((mapping.get(c, c) for c in S))
		return reversed_s


class Solution:

	def magicalString(ob, S):
		ans = []
		for i in S:
			if 'a' <= i and i <= 'z':
				i = chr(ord('a') + ord('z') - ord(i))
				ans.append(i)
		return ''.join(ans)


class Solution:

	def magicalString(ob, s):
		List1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm']
		List2 = ['z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'q', 'p', 'o', 'n']
		final_List = []
		Output = ''
		for i in s:
			if i in List1:
				Indxe = List1.index(i)
				final_List.append(List2[Indxe])
			else:
				Indxe = List2.index(i)
				final_List.append(List1[Indxe])
		for i in final_List:
			Output += i
		return Output


class Solution:

	def magicalString(ob, S):
		v = ''
		for i in S:
			k = ord(i)
			kk = ord(i) - 97
			kkk = 122 - kk
			v += chr(kkk)
		return v


class Solution:

	def magicalString(ob, S):
		res = ''
		for i in S:
			temp = chr(ord('z') - (ord(i) - ord('a')))
			res += temp
		return res


class Solution:

	def magicalString(ob, S):
		res = []
		for i in range(len(S)):
			x = ord(S[i])
			if x >= 97 and x <= 108:
				x -= 97
				x = 122 - x
			elif x >= 109 and x <= 122:
				x = 122 - x
				x = 97 + x
			res.append(chr(x))
		S = ''
		for i in res:
			S += i
		return S


class Solution:

	def magicalString(ob, S):
		ans = ''
		for i in range(len(S)):
			c = chr(ord('z') - ord(S[i]) + ord('a'))
			ans += c
		return ans


class Solution:

	def magicalString(ob, S):
		letters = 'abcdefghijklmnopqrstuvwxyz'
		new = ''
		for i in S:
			if i in letters:
				ind = letters.index(i)
				new += letters[-ind - 1]
		return new


class Solution:

	def magicalString(ob, S):
		ch = ''
		for i in S:
			if 'A' <= i and i <= 'Z':
				ch = ch + chr(ord('A') + ord('Z') - ord(i))
			elif 'a' <= i and i <= 'z':
				ch = ch + chr(ord('a') + ord('z') - ord(i))
			else:
				ch = ch + ' '
		return ch


class Solution:

	def magicalString(ob, S):
		string = ''
		for index in range(len(S)):
			string += chr(122 - (ord(S[index]) - ord('a')))
		return string


class Solution:

	def magicalString(ob, S):
		output = ''
		for item in S:
			output += chr(219 - ord(item))
		return output


class Solution:

	def magicalString(ob, S):
		str1 = ''
		for i in range(len(S)):
			str1 += chr(219 - ord(S[i]))
		return str1


class Solution:

	def magicalString(ob, s):
		mapping = {'a': 'z', 'b': 'y', 'c': 'x', 'd': 'w', 'e': 'v', 'f': 'u', 'g': 't', 'h': 's', 'i': 'r', 'j': 'q', 'k': 'p', 'l': 'o', 'm': 'n', 'n': 'm', 'o': 'l', 'p': 'k', 'q': 'j', 'r': 'i', 's': 'h', 't': 'g', 'u': 'f', 'v': 'e', 'w': 'd', 'x': 'c', 'y': 'b', 'z': 'a'}
		result = ''.join((mapping.get(i, i) for i in s))
		return result


class Solution:

	def magicalString(ob, S):
		a = 'abcdefghijklmnopqrstuvwxyz'
		w = ''
		for i in S:
			w = w + a[25 - a.index(i)]
		return w


class Solution:

	def magicalString(ob, S):
		ordered = 'abcdefghijklmnopqrstuvwxyz'
		unordered = 'zyxwvutsrqponmlkjihgfedcba'
		new = ''
		for i in S:
			if i in ordered:
				index = ordered.index(i)
				value = unordered[index]
				new += value
		return new


class Solution:

	def magicalString(ob, S):
		str_rev = ''
		for i in S:
			if 97 <= ord(i) <= 122:
				rev = ord(i) - 97
				str_rev += chr(122 - rev)
		return str_rev


class Solution:

	def magicalString(ob, S):
		l = ''
		for i in range(len(S)):
			l += chr(ord('a') + ord('z') - ord(S[i]))
		return l


class Solution:

	def magicalString(ob, S):
		sam = {'a': 'z', 'b': 'y', 'c': 'x', 'd': 'w', 'e': 'v', 'f': 'u', 'g': 't', 'h': 's', 'i': 'r', 'j': 'q', 'k': 'p', 'l': 'o', 'm': 'n', 'n': 'm', 'o': 'l', 'p': 'k', 'q': 'j', 'r': 'i', 's': 'h', 't': 'g', 'u': 'f', 'v': 'e', 'w': 'd', 'x': 'c', 'y': 'b', 'z': 'a'}
		emp = ''
		for i in S:
			if i in sam.keys():
				emp = emp + sam[i]
		return emp


class Solution:

	def magicalString(ob, S):
		a = []
		for i in range(97, 123):
			a.append(chr(i))
		b = []
		for i in range(122, 96, -1):
			b.append(chr(i))
		d = dict(zip(a, b))
		ans = ''
		for i in S:
			ans += d[i]
		return ans


class Solution:

	def magicalString(ob, S):
		a = ''
		for i in S:
			if ord(i) >= 65 and ord(i) <= 90:
				c = ord(i) - 65
				d = 90 - c
				a += chr(d)
			elif ord(i) >= 97 and ord(i) <= 122:
				c = ord(i) - 97
				d = 122 - c
				a += chr(d)
			else:
				a += i
		return a


class Solution:

	def magicalString(ob, S):
		l = ''
		for i in S:
			z = 0
			s = 0
			z = ord(i) - 97
			s = 122 - z
			l += chr(s)
		return l


class Solution:

	def magicalString(ob, S):
		a = 'abcdefghijklmnopqrstuvwxyz'
		b = ''
		for element in S:
			c = a.index(element)
			d = 25 - c
			b += a[d]
		return b


class Solution:

	def magicalString(ob, S):
		output = ''
		for char in S:
			swap_char = chr(122 - (ord(char) - 97))
			output += swap_char
		return output


class Solution:

	def magicalString(ob, S):
		num = []
		a = 'abcdefghijklmnopqrstuvwxyz'
		b = a[::-1]
		for i in S:
			if i in a:
				c = a.index(i)
				d = b[c]
				num.append(d)
		s = ''
		for j in num:
			s += j
		return s


class Solution:

	def magicalString(ob, S):
		s = ''
		m = S
		for i in S:
			if i.islower():
				temp = 123 - (ord(i) - 96)
				s += chr(temp)
		return s


class Solution:

	def magicalString(ob, S):
		a = 'abcdefghijklmnopqrstuvwxyz'
		res = ''
		for i in S:
			A = a.index(i)
			res = res + a[-(A + 1)]
		return res


class Solution:

	def magicalString(ob, S):
		st = 'abcdefghijklmnopqrstuvwxyz'
		ans = ''
		for x in S:
			ans += st[-1 - st.find(x)]
		return ans


class Solution:

	def magicalString(ob, S):
		alpha = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
		ans = ''
		for i in S:
			ans += alpha[ord('z') - ord(i)]
		return ans


class Solution:

	def magicalString(ob, S):
		st = ''
		s = ''
		for i in range(len(S)):
			st = chr(ord('z') - ord(S[i]) + ord('a'))
			s = s + st
		return s


class Solution:

	def magicalString(ob, S):
		a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
		c = []
		for i in range(len(S)):
			b = a.index(S[i]) + 1
			c.append(a[abs(b - 26)])
		return ''.join(c)


class Solution:

	def magicalString(ob, S):
		right = 'abcdefghijklmnopqrstuvwxyz'
		left = right[::-1]
		answer = ''
		for i in S:
			answer += left[right.index(i)]
		return answer


class Solution:

	def magicalString(ob, S):
		out = ''
		for i in S:
			out += chr(ord('z') - (ord(i) - ord('a')))
		return out


class Solution:

	def magicalString(ob, S):
		l1 = []
		l2 = []
		for i in range(97, 123):
			if i < 110:
				l1.append(chr(i))
			else:
				l2.append(chr(i))
		l2 = l2[::-1]
		s2 = ''
		for i in S:
			if i not in l1:
				s2 = s2 + l1[l2.index(i)]
			else:
				s2 = s2 + l2[l1.index(i)]
		return s2


class Solution:

	def magicalString(ob, S):
		a = 'abcdefghijklmnopqrstuvwxyz'
		b = list(a)
		d = {}
		l = len(b)
		for i in range(l):
			d[a[i]] = a[l - 1 - i]
		return ''.join([d[s] for s in S])


class Solution:

	def magicalString(ob, S):
		res = ''
		for i in S:
			num = ord(i)
			if i == ' ':
				res += ' '
			elif num >= 65 and num <= 90:
				rem = num - 65
				ans = 90 - rem
				let = chr(ans)
				res += let
			elif num >= 97 and num <= 122:
				rem = num - 97
				ans = 122 - rem
				let = chr(ans)
				res += let
		return res


class Solution:

	def magicalString(ob, S):
		a = 'abcdefghijklmnopqrstuvwxyz'
		b = a[::-1]
		str1 = ''
		for i in S:
			str1 += b[a.index(i)]
		return str1


class Solution:

	def magicalString(ob, S):
		alpha = 'abcdefghijklmnopqrstuvwxyz'
		rev = alpha[::-1]
		ans = ''
		for i in S:
			if i in alpha:
				val = alpha.index(i)
				ans += rev[val]
		return ans


class Solution:

	def magicalString(ob, S):
		asc = 'abcdefghijklmnopqrstuvwxyz'
		dsc = asc[::-1]
		final = ''
		for i in S:
			final += dsc[asc.index(i)]
		return final


class Solution:

	def magicalString(ob, S):
		res = ''
		for x in S:
			res += chr(219 - ord(x))
		return res


class Solution:

	def magicalString(ob, S):
		s = ''
		for i in range(len(S)):
			x = ord(S[i])
			y = 122 - x
			s += chr(97 + y)
		return s


import string

class Solution:

	def magicalString(ob, S):
		a = string.ascii_letters
		b = 'zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA'
		table = S.maketrans(a, b)
		return S.translate(table)


class Solution:

	def magicalString(ob, S):
		alpha = 'abcdefghijklmnopqrstuvwxyz'
		magical_str = ''
		for i in S:
			if i in alpha:
				magical_str += alpha[-(alpha.index(i) + 1)]
		return magical_str


class Solution:

	def magicalString(ob, S):
		s1 = 'abcdefghijklmnopqrstuvwxyz'
		s2 = 'zyxwvutsrqponmlkjihgfedcba'
		r = ''
		for i in range(len(S)):
			if S[i] in s1:
				r += s2[s1.rfind(S[i])]
		return r


class Solution:

	def magicalString(ob, S):
		alp = 'a b c d e f g h i j k l m n o p q r s t u v w x y z'.split(' ')
		rev_alp = alp[::-1]
		d = dict(list(zip(alp, rev_alp)))
		res = ''
		for i in S:
			res += d[i]
		return res


class Solution:

	def magicalString(ob, S):
		res = ''
		for i in S:
			res += chr(abs(122 - ord(i) + 97))
		return res


class Solution:

	def magicalString(ob, S):
		map_1 = {'a': 'z', 'b': 'y', 'c': 'x', 'd': 'w', 'e': 'v', 'f': 'u', 'g': 't', 'h': 's', 'i': 'r', 'j': 'q', 'k': 'p', 'l': 'o', 'm': 'n'}
		map_2 = {v: k for (k, v) in map_1.items()}
		magic_s = ''
		for c in S:
			if c in map_1:
				new_c = map_1[c]
			else:
				new_c = map_2[c]
			magic_s += new_c
		return magic_s


class Solution:

	def magicalString(ob, S):
		string = 'abcdefghijklmnopqrstuvwxyz'
		t = ''
		for i in S:
			c = string.find(i)
			if c == -1:
				t += i
			elif c > 13:
				t += string[25 - c]
			else:
				t += string[-c - 1]
		return t


class Solution:

	def magicalString(ob, S):
		new = ''
		for x in S:
			new += chr(abs(122 - ord(x) + 97))
		return new


class Solution:

	def magicalString(ob, S: str) -> str:
		import string
		letters = string.ascii_lowercase
		_dict = {k: v for (k, v) in zip(letters, letters[::-1])}
		return ''.join((_dict[x] for x in S))


class Solution:

	def magicalString(ob, S):
		arr = ''
		for i in S:
			if ord(i) >= 97 and ord(i) <= 122:
				arr = arr + chr(122 - (ord(i) - 97))
		return arr


class Solution:

	def magicalString(ob, S):
		letter = 'abcdefghijklmnopqrstuvwxyz'
		ans = ''
		for i in S:
			ans += letter[97 - ord(i) - 1]
		return ans


class Solution:

	def magicalString(ob, S):
		l = []
		for i in S:
			l.append(chr(97 + 122 - ord(i)))
		return ''.join(l)


class Solution:

	def magicalString(ob, S):
		result = ''
		for i in S:
			result += chr(122 - (ord(i) - 97 + 1) + 1)
		return result


import string

class Solution:

	def magicalString(ob, S):
		a = ''.join(list(string.ascii_lowercase))
		b = a[::-1]
		result = ''
		temp = 0
		for i in S:
			temp = a.find(i)
			result = result + b[temp]
			temp = 0
		return result


class Solution:

	def magicalString(ob, S):
		b = 'abcdefghijklmnopqrstuvwxyz'
		size = len(S)
		ans = ''
		for i in range(size):
			d = ord(S[i]) - ord('a')
			ans += b[25 - d]
		return ans


class Solution:

	def magicalString(ob, S):
		ans = ''
		for i in S:
			temp = 123 - (ord(i) - 96)
			ans += chr(temp)
		return ans


class Solution:

	def magicalString(ob, S):
		m = 'abcdefghijklmnopqrstuvwxyz'
		s = ''
		for i in S:
			t = m.index(i)
			s += m[-t - 1]
		return s


class Solution:

	def magicalString(ob, S):
		m = 'abcdefghijklmnopqrstuvwxyz'
		s = ''
		for x in S:
			ind = m.index(x)
			s += m[-ind - 1]
		return s


class Solution:

	def magicalString(ob, S):
		s = ''
		for i in S:
			temp = ord(i)
			if temp < 110:
				temp = 110 - temp
				s += chr(109 + temp)
			if temp == 110:
				s += 'm'
			if temp > 110:
				temp = 122 - temp
				s += chr(97 + temp)
		return s


class Solution:

	def magicalString(ob, s):
		x = l[::-1]
		new_string = ''
		for i in s:
			a = ord(i)
			new_string += x[a - 97]
		return new_string
l = []
for i in range(97, 123):
	l += [chr(i)]


class Solution:

	def magicalString(ob, S):
		return ''.join([chr(122 - (ord(x) - 97)) for x in S])


class Solution:

	def magicalString(ob, S):
		l = ''
		n = 0
		for i in S:
			n = 122 - ord(i) + 97
			l += chr(n)
		return l