Given to strings B and G. String B represents a binary code and string G represents a Gray Code. You need to write a program which converts binary code to gray code and vice versa.
 
Example 1:
Input:
B = "0011"
G = "01101"
Output:
0010
01001
Explanation:
Grey Code of 0011 is 0010.
Binary of 01101 is 01001.
Example 2:
Input:
B = "01001"
G = "01101"
Output:
01101
01001
Explanation:
Grey Code of 01001 is 01101.
Binary of 01101 is 01001.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the functions binToGrey() and greyToBin() which takes Strings B and G as input and returns the answers.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= |B| <= 10^{5}
1 <= |G| <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Starter code: #User function Template for python3



class Solution:

    def binToGrey(self, B):

        # code here 

    def greyToBin(self, G):

        # code here 

Solutions:
class Solution:

	def binToGrey(self, B):
		G = ''
		G = G + B[0]
		for i in range(1, len(B)):
			G = G + str((int(B[i - 1]) + int(B[i])) % 2)
		return G

	def greyToBin(self, G):
		B = ''
		B = B + G[0]
		for i in range(1, len(G)):
			B = B + str((int(B[-1]) + int(G[i])) % 2)
		return B


class Solution:

	def binToGrey(self, B):
		l = len(B)
		B = int(B, 2)
		res = bin(B ^ B >> 1)[2:]
		if len(res) < l:
			res = '0' * (l - len(res)) + res
		return res

	def greyToBin(self, G):
		l = len(G)
		G = int(G, 2)
		mask = G >> 1
		while mask != 0:
			G = G ^ mask
			mask = mask >> 1
		res = bin(G)[2:]
		if len(res) < l:
			res = '0' * (l - len(res)) + res
		return res


class Solution:

	def binToGrey(self, B):

		def xor(a, b):
			if a == b:
				return '0'
			else:
				return '1'
		grey = B[0]
		for i in range(1, len(B)):
			grey += xor(int(B[i - 1]), int(B[i]))
		return grey

	def greyToBin(self, G):
		bin1 = G[0]
		for i in range(1, len(G)):
			if G[i] == '0':
				bin1 += bin1[i - 1]
			elif bin1[i - 1] == '1':
				bin1 += '0'
			else:
				bin1 += '1'
		return bin1


class Solution:

	def b_xor(self, a, b):
		return '1' if a != b else '0'

	def flip(self, c):
		return '0' if c == '1' else '1'

	def binToGrey(self, B):
		gray = ''
		gray += B[0]
		for i in range(1, len(B)):
			gray += self.b_xor(B[i - 1], B[i])
		return gray

	def greyToBin(self, G):
		binary = ''
		binary += G[0]
		for i in range(1, len(G)):
			if G[i] == '0':
				binary += binary[i - 1]
			else:
				binary += self.flip(binary[i - 1])
		return binary


class Solution:

	def binToGrey(self, B):
		grey = ''
		grey += B[0]
		for i in range(1, len(B)):
			grey += str(int(B[i - 1]) ^ int(B[i]))
		return grey

	def greyToBin(self, G):
		binary = ''
		binary += G[0]
		for i in range(1, len(G)):
			if G[i] == '0':
				binary += binary[i - 1]
			elif binary[i - 1] == '0':
				binary += '1'
			else:
				binary += '0'
		return binary


class Solution:

	def binToGrey(self, B):
		length = len(B)
		n = int(B, 2)
		p = n ^ n >> 1
		return bin(p)[2:].zfill(length)

	def greyToBin(self, G):
		length = len(G)
		n = int(G, 2)
		r = n
		while n > 0:
			n = n >> 1
			r = r ^ n
		return bin(r)[2:].zfill(length)


class Solution:

	def binToGrey(self, B):
		n = len(B)
		grey_code = B[0]
		for i in range(0, n - 1):
			char = str(int(B[i]) ^ int(B[i + 1]))
			grey_code += char
		return grey_code

	def greyToBin(self, G):
		n = len(G)
		bin_code = G[0]
		for i in range(1, n):
			char = str(int(bin_code[i - 1]) ^ int(G[i]))
			bin_code += char
		return bin_code
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		B = input()
		G = input()
		ob = Solution()
		print(ob.binToGrey(B))
		print(ob.greyToBin(G))


class Solution:

	def binToGrey(self, B):
		t = B[0]
		for i in range(1, len(B)):
			if B[i - 1] == B[i]:
				t += '0'
			else:
				t += '1'
		return t

	def greyToBin(self, G):
		d = len(G)
		t = G[0]
		g = G[0]
		for i in range(1, d):
			if g == G[i]:
				g = '0'
			else:
				g = '1'
			t += g
		return t
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		B = input()
		G = input()
		ob = Solution()
		print(ob.binToGrey(B))
		print(ob.greyToBin(G))


class Solution:

	def binToGrey(self, B):
		resBtoG = [B[0]]
		for i in range(1, len(B)):
			resBtoG.append(str(int(B[i - 1]) ^ int(B[i])))
		return ''.join(resBtoG)

	def greyToBin(self, G):
		resGtoB = [G[0]]
		for i in range(1, len(G)):
			resGtoB.append(str(int(resGtoB[i - 1]) ^ int(G[i])))
		return ''.join(resGtoB)