Given two numbers in the form of Strings. Find the count of carries in their addition.
 
Example 1:
Input:
N = "34", M = "25"
Output:
0
Explanation:
There are no carries when N and M
are added.
Example 2:
Input:
N = "4634", M = "1757"
Output:
2
Explanation:
There are 2 carries when N and M
are added.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function getCarries() which takes two Strings N and M as input and returns an integer denoting the answer.
 
Expected Time Complexity: O(|N|)
Expected Auxiliary Space: O(|N|)
 
Constraints:
1 <= |N|,|M| <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Starter code: #User function Template for python3

class Solution:
    def getCarries(self, N, M):
        # code here 

Solutions:
class Solution:

	def getCarries(self, N, M):
		carries = 0
		no_of_carries = 0
		if len(M) >= len(N):
			max = len(M)
			min = len(N)
			for i in range(0, max):
				if i < min:
					summ = int(M[-i - 1]) + int(N[-i - 1]) + carries
					if summ >= 10:
						carries = 1
						no_of_carries += 1
					else:
						carries = 0
				else:
					summ = int(M[-i - 1]) + carries
					if summ >= 10:
						carries = 1
						no_of_carries += 1
					else:
						carries = 0
			return no_of_carries
		elif len(N) > len(M):
			max = len(N)
			min = len(M)
			for i in range(0, max):
				if i < min:
					summ = int(M[-i - 1]) + int(N[-i - 1]) + carries
					if summ >= 10:
						carries = 1
						no_of_carries += 1
					else:
						carries = 0
				else:
					summ = int(N[-i - 1]) + carries
					if summ >= 10:
						carries = 1
						no_of_carries += 1
					else:
						carries = 0
			return no_of_carries


class Solution:

	def getCarries(self, N, M):
		carry = 0
		c = 0
		l1 = len(N)
		l2 = len(M)
		while l1 > 0 or l2 > 0:
			x = 0
			y = 0
			if l1 > 0:
				x = int(N[l1 - 1])
			if l2 > 0:
				y = int(M[l2 - 1])
			l1 = l1 - 1
			l2 = l2 - 1
			s = x + y + carry
			if s >= 10:
				carry = 1
				c = c + 1
			else:
				carry = 0
		return c


class Solution:

	def getCarries(self, N, M):
		if len(M) != len(N):
			if len(M) > len(N):
				N = '0' * (len(M) - len(N)) + N
			else:
				M = '0' * (len(N) - len(M)) + M
			c = 0
			k = 0
			for i in range(len(N) - 1, -1, -1):
				if int(N[i]) + int(M[i]) + k > 9:
					k = 1
					c = c + 1
				else:
					k = 0
			return c
		else:
			c = 0
			k = 0
			for i in range(len(N) - 1, -1, -1):
				if int(N[i]) + int(M[i]) + k > 9:
					k = 1
					c = c + 1
				else:
					k = 0
			return c


class Solution:

	def getCarries(self, N, M):
		r = 0
		rem = 0
		x = 0
		N = N[::-1]
		M = M[::-1]
		for (i, j) in zip(N, M):
			a = int(i) + int(j) + rem
			if a > 9:
				r += 1
				rem = 1
			else:
				rem = 0
		n1 = len(N)
		n2 = len(M)
		if n1 != n2:
			if n1 > n2:
				arr = N[n2:]
			else:
				arr = M[n1:]
			n = len(arr)
			i = 0
			while i < n:
				a = int(arr[i]) + rem
				if a > 9:
					r += 1
					rem = 1
				else:
					rem = 0
					break
				i += 1
		return r


class Solution:

	def getCarries(self, N, M):
		cnt = 0
		carry = 0
		(i, j) = (len(N) - 1, len(M) - 1)
		while i >= 0 and j >= 0:
			carry += int(N[i]) + int(M[j])
			carry //= 10
			if carry:
				cnt += 1
			i -= 1
			j -= 1
		while i >= 0:
			carry += int(N[i])
			carry //= 10
			if carry:
				cnt += 1
			i -= 1
		while j >= 0:
			carry += int(M[j])
			carry //= 10
			if carry:
				cnt += 1
			j -= 1
		return cnt


class Solution:

	def getCarries(self, N, M):
		carry = 0
		count = 0
		while len(N) < len(M):
			N = '0' + N
		while len(M) < len(N):
			M = '0' + M
		for i in range(len(M) - 1, -1, -1):
			added = int(M[i]) + int(N[i])
			if added > 9:
				count += 1
				carry = 1
			elif added + carry > 9:
				count += 1
				carry = 1
			elif carry == 1:
				carry = 0
		return count


class Solution:

	def getCarries(self, N, M):
		while len(N) < len(M):
			N = '0' + N
		while len(M) < len(N):
			M = '0' + M
		carry = 0
		count = 0
		for i in range(len(M) - 1, -1, -1):
			num = int(M[i]) + int(N[i])
			if num >= 10:
				carry = 1
				count += 1
			elif num + carry >= 10:
				carry == 1
				count += 1
			elif carry == 1:
				carry = 0
		return count


class Solution:

	def getCarries(self, a, b):
		carry = 0
		count = 0
		len_a = len(a)
		len_b = len(b)
		while len_a != 0 or len_b != 0:
			x = 0
			y = 0
			if len_a > 0:
				x = int(a[len_a - 1]) + int('0')
				len_a -= 1
			if len_b > 0:
				y = int(b[len_b - 1]) + int('0')
				len_b -= 1
			sum = x + y + carry
			if sum >= 10:
				carry = 1
				count += 1
			else:
				carry = 0
		return count


class Solution:

	def getCarries(self, N, M):
		if len(N) > len(M):
			a = len(N) - len(M)
			for i in range(0, a):
				M = '0' + M
		elif len(N) < len(M):
			a = len(M) - len(N)
			for i in range(0, a):
				N = '0' + N
		count = 0
		d = 0
		M = M[::-1]
		N = N[::-1]
		for w in range(0, len(M)):
			if int(M[w]) + int(N[w]) + d >= 10:
				count += 1
				d = 1
			else:
				d = 0
		return count


class Solution:

	def addleftzeros(self, str1, count, str2):
		i = 0
		mystr = str1
		while i < count:
			mystr = str2 + mystr
			i += 1
		return mystr

	def getCarries(self, N, M):
		if len(N) > len(M):
			n1 = N
			m1 = M
		else:
			n1 = M
			m1 = N
		m1 = self.addleftzeros(m1, len(n1) - len(m1), '0')
		i = 0
		count = 0
		carry_1 = 0
		while i < len(n1):
			if int(n1[len(n1) - i - 1]) + int(m1[len(m1) - i - 1]) + carry_1 > 9:
				count += 1
				carry_1 = 1
			else:
				carry_1 = 0
			i += 1
		return count


class Solution:

	def getCarries(self, N, M):
		if len(N) > len(M):
			N1 = N
			M1 = M
		else:
			N1 = M
			M1 = N
		r = 0
		a = len(N1) - len(M1)
		while r < a:
			M1 = '0' + M1
			r += 1
		count = 0
		i = 0
		carry = 0
		while i < len(N1):
			if int(N1[len(N1) - i - 1]) + int(M1[len(M1) - i - 1]) + carry > 9:
				count = count + 1
				carry = 1
			else:
				carry = 0
			i += 1
		return count


class Solution:

	def addleftzero(self, str1, count, str2):
		i = 0
		mystr = str1
		while i < count:
			mystr = str2 + mystr
			i += 1
		return mystr

	def getCarries(self, N, M):
		if len(N) > len(M):
			N1 = N
			M1 = M
		else:
			N1 = M
			M1 = N
		M1 = self.addleftzero(M1, len(N1) - len(M1), '0')
		i = 0
		count = 0
		Carry1 = 0
		while i < len(N1):
			if int(N1[len(N1) - i - 1]) + int(M1[len(M1) - i - 1]) + Carry1 > 9:
				count = count + 1
				Carry1 = 1
			else:
				Carry1 = 0
			i += 1
		return count


class Solution:

	def getCarries(self, N, M):
		carry = 0
		count = 0
		p = len(N)
		q = len(M)
		while p != 0 or q != 0:
			(x, y) = (0, 0)
			if p > 0:
				x = int(N[p - 1]) + int('0')
				p = p - 1
			if q > 0:
				y = int(M[q - 1]) + int('0')
				q = q - 1
			s = x + y + carry
			if s >= 10:
				carry = 1
				count += 1
			else:
				carry = 0
		return count


class Solution:

	def getCarries(self, N, M):
		c = 0
		d = 0
		e = len(N)
		f = len(M)
		g = max(f, e)
		N = (g - e) * '0' + N
		M = (g - f) * '0' + M
		N = N[::-1]
		M = M[::-1]
		for i in range(g):
			g = c + int(N[i]) + int(M[i])
			if g >= 10:
				c = 1
				d += 1
			else:
				c = 0
		return d


class Solution:

	def getCarries(self, N, M):
		N = list(N)
		M = list(M)
		mini = min(len(N), len(M))
		maxi = max(len(N), len(M))
		carry = 0
		count = 0
		ln = len(N)
		for i in range(mini):
			x = int(N[len(N) - 1 - i]) + int(M[len(M) - 1 - i]) + count
			count = 0
			if x >= 10:
				count = x // 10
				carry += x // 10
		if maxi == len(N):
			for i in range(maxi - mini):
				x = int(N[maxi - mini - i - 1]) + count
				count = 0
				if x >= 10:
					count = x // 10
					carry += 1
				else:
					break
		else:
			for i in range(maxi - mini):
				x = int(M[maxi - mini - i - 1]) + count
				count = 0
				if x >= 10:
					count = x // 10
					carry += 1
				else:
					break
		return carry


class Solution:

	def getCarries(self, n, m):
		if len(n) >= len(m):
			count = 0
			carry = 0
			for i in range(len(m)):
				s = int(n[len(n) - i - 1]) + int(m[len(m) - 1 - i]) + carry
				if s >= 10:
					count += 1
					carry = 1
				else:
					carry = 0
			for i in range(len(m), len(n)):
				s = int(n[len(n) - 1 - i]) + carry
				if s >= 10:
					count += 1
					carry = 1
				else:
					carry = 0
		else:
			count = 0
			carry = 0
			for i in range(len(n)):
				s = int(n[len(n) - i - 1]) + int(m[len(m) - 1 - i]) + carry
				if s >= 10:
					count += 1
					carry = 1
				else:
					carry = 0
			for i in range(len(n), len(m)):
				s = int(m[len(m) - 1 - i]) + carry
				if s >= 10:
					count += 1
					carry = 1
				else:
					carry = 0
		return count
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(N, M) = map(str, input().split())
		ob = Solution()
		print(ob.getCarries(N, M))


class Solution:

	def getCarries(self, a, b):
		carry = 0
		count = 0
		len_a = len(a)
		len_b = len(b)
		while len_a != 0 or len_b != 0:
			x = 0
			y = 0
			if len_a > 0:
				x = int(a[len_a - 1]) + int('0')
				len_a -= 1
			if len_b > 0:
				y = int(b[len_b - 1]) + int('0')
				len_b -= 1
			sum = x + y + carry
			if sum >= 10:
				carry = 1
				count += 1
			else:
				carry = 0
		return count
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(N, M) = map(str, input().split())
		ob = Solution()
		print(ob.getCarries(N, M))


class Solution:

	def getCarries(self, N, M):
		N = list(N)
		M = list(M)
		mini = min(len(N), len(M))
		maxi = max(len(N), len(M))
		carry = 0
		count = 0
		ln = len(N)
		for i in range(mini):
			x = int(N[len(N) - 1 - i]) + int(M[len(M) - 1 - i]) + count
			count = 0
			if x >= 10:
				count = x // 10
				carry += x // 10
		if maxi == len(N):
			for i in range(maxi - mini):
				x = int(N[maxi - mini - i - 1]) + count
				count = 0
				if x >= 10:
					count = x // 10
					carry += 1
				else:
					break
		else:
			for i in range(maxi - mini):
				x = int(M[maxi - mini - i - 1]) + count
				count = 0
				if x >= 10:
					count = x // 10
					carry += 1
				else:
					break
		return carry
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(N, M) = map(str, input().split())
		ob = Solution()
		print(ob.getCarries(N, M))


class Solution:

	def getCarries(self, N, M):
		i = len(N) - 1
		j = len(M) - 1
		carrycount = 0
		carry = 0
		while i >= 0 or j >= 0:
			temp = 0
			if i >= 0:
				temp = temp + int(N[i])
				i = i - 1
			if j >= 0:
				temp = temp + int(M[j])
				j = j - 1
			temp = temp + carry
			carry = temp // 10
			if carry > 0:
				carrycount += 1
		return carrycount


class Solution:

	def getCarries(self, N, M):
		if len(N) < len(M):
			(M, N) = (N, M)
		carry = 0
		count = 0
		for i in range(len(M)):
			a = carry + int(N[-i - 1]) + int(M[-i - 1])
			if a >= 10:
				carry = 1
				count += 1
			else:
				carry = 0
		for i in range(len(N) - len(M)):
			a = carry + int(N[-len(M) - i - 1])
			if a >= 10:
				carry = 1
				count += 1
			else:
				carry = 0
		return count


class Solution:

	def getCarries(self, N, M):
		x = len(N) - 1
		y = len(M) - 1
		carry = 0
		count = 0
		while x >= 0 or y >= 0:
			(val1, val2) = (0, 0)
			if x >= 0:
				val1 = int(N[x])
				x -= 1
			if y >= 0:
				val2 = int(M[y])
				y -= 1
			summ = val1 + val2 + carry
			carry = summ // 10
			if carry > 0:
				count += 1
		return count


class Solution:

	def getCarries(self, N, M):
		ln = len(N) - 1
		lm = len(M) - 1
		if ln == -1 or lm == -1:
			return 0
		c = 0
		p = 0
		while ln >= 0 and lm >= 0:
			v = (int(N[ln]) + int(M[lm]) + c) // 10
			if v == 1:
				p += 1
			ln -= 1
			lm -= 1
			c = v
		while ln >= 0:
			v = (int(N[ln]) + c) // 10
			if v == 1:
				p += 1
			else:
				break
			c = v
			ln -= 1
		while lm >= 0:
			v = (int(M[lm]) + c) // 10
			if v == 1:
				p += 1
			else:
				break
			c = v
			lm -= 1
		return p


class Solution:

	def getCarries(self, N, M):
		carry = 0
		count = 0
		x = len(N)
		y = len(M)
		while x != 0 or y != 0:
			l = 0
			m = 0
			if x > 0:
				l = int(N[x - 1]) + int('0')
				x = x - 1
			if y > 0:
				m = int(M[y - 1]) + int('0')
				y = y - 1
			s = l + m + carry
			if s >= 10:
				carry = 1
				count += 1
			else:
				carry = 0
		return count


class Solution:

	def getCarries(self, N, M):
		c = 0
		j = 0
		f = 0
		if len(M) > len(N):
			a = N
			N = M
			M = a
		N = N[::-1]
		M = M[::-1]
		for i in N:
			if j >= len(M):
				a = int(i) + c
			else:
				a = int(i) + int(M[j]) + c
			if a > 9:
				f = f + 1
				c = 1
			else:
				c = 0
			j = j + 1
		return f


class Solution:

	def getCarries(self, N, M):
		count = 0
		carry = 0
		len_n = len(N)
		len_m = len(M)
		while len_n != 0 or len_m != 0:
			x = 0
			y = 0
			if len_m > 0:
				x = int(M[len_m - 1]) + int('0')
				len_m -= 1
			if len_n > 0:
				y = int(N[len_n - 1]) + int('0')
				len_n -= 1
			s = x + y + carry
			if s >= 10:
				carry = 1
				count += 1
			else:
				carry = 0
		return count