Given two numbers N1 and N2, the task is to swap the first digit of the first number with the last digit of the second number and last digit of the first number with the first digit of the second number.
Example 1:
Input:  N1 = "1234", N2 = "5678" 
Output: 8235 4671
Explanation: swap {1, 8} and {4, 5}
Example 2:
Input: N1 = "555", N2 = "55"
Output: 555 55
Explanation: swap {5, 5} and {5, 5}
User Task:
Your task is to complete the function swapDigits() which takes two strings as input and modifies given strings. You do not need to take any input or print anything.
Note: In java code, a custom class Str is used instead of String data type because String in java is passed by value.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
2 <= |N1|, |N2| <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:
    def swapDigits(self, n1, n2):
        # code here

Solutions:
class Solution:

	def swapDigits(self, n1, n2):
		n1 = [*n1]
		n2 = [*n2]
		(n1[0], n2[-1]) = (n2[-1], n1[0])
		(n1[-1], n2[0]) = (n2[0], n1[-1])
		global s
		global n
		s = ''.join(n1)
		n = ''.join(n2)
		return


class Solution:

	def swapDigits(self, n1, n2):
		s1 = n2[-1] + n1[1:-1] + n2[0]
		s2 = n1[-1] + n2[1:-1] + n1[0]
		global s, n
		(s, n) = (s1, s2)


class Solution:

	def swapDigits(self, n1, n2):
		(a, b, c, d) = (n1[0], n1[-1], n2[0], n2[-1])
		n1 = d + n1[1:len(n1) - 1] + c
		n2 = b + n2[1:len(n2) - 1] + a
		global s, n
		(s, n) = (n1, n2)


class Solution:

	def swapDigits(self, n1, n2):
		l1 = []
		l2 = []
		for i in n1:
			l1.append(i)
		for i in n2:
			l2.append(i)
		(l1[0], l2[-1]) = (l2[-1], l1[0])
		(l1[-1], l2[0]) = (l2[0], l1[-1])
		return [''.join(l1), ''.join(l2)]


class Solution:

	def swapDigits(self, n1, n2):
		lst1 = list(n1)
		lst2 = list(n2)
		lst1[0] = n2[-1]
		lst1[-1] = n2[0]
		lst2[0] = n1[-1]
		lst2[-1] = n1[0]
		n1 = ''.join(lst1)
		n2 = ''.join(lst2)
		global s, n
		(s, n) = (n1, n2)


class Solution:

	def swapDigits(self, n1, n2):
		tmp = n1
		tmp = n1
		n1 = n2[-1] + n1[1:-1] + n2[0]
		n2 = tmp[-1] + n2[1:-1] + tmp[0]
		global s, n
		(s, n) = (n1, n2)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input().strip()
		n = input().strip()
		ob = Solution()
		ob.swapDigits(s, n)
		print(s, n)


class Solution:

	def swapDigits(self, n1, n2):
		tmp = n1
		tmp = n1
		n1 = n2[-1] + n1[1:-1] + n2[0]
		n2 = tmp[-1] + n2[1:-1] + tmp[0]
		global s, n
		(s, n) = (n1, n2)


class Solution:

	def swapDigits(self, n1, n2):
		x = len(n1)
		y = len(n2)
		a = n2[-1] + n1[1:x - 1] + n2[0]
		b = n1[-1] + n2[1:y - 1] + n1[0]
		n1 = a
		n2 = b
		global s, n
		s = n1
		n = n2


if __name__ == '__main__':
	for t in range(int(input())):
		n1 = input().strip()
		n2 = input().strip()
		print(n2[-1] + n1[1:-1] + n2[0], end=' ')
		print(n1[-1] + n2[1:-1] + n1[0])
quit()


class Solution:

	def swapDigits(self, n1, n2):
		f1 = n1[0]
		l1 = n1[-1]
		f2 = n2[0]
		l2 = n2[-1]
		global s
		s = l2 + n1[1:-1] + f2
		global n
		n = l1 + n2[1:-1] + f1