Given a range of numbers starting with integer L and ending at R, the task is to count the numbers which have same first and last digits.
 
Example 1:
Input:
L = 7 
R = 68
Output:
9
Explanation:
Number with the same 
first and last digits 
in the given range is:
[7, 8, 9, 11, 22, 33, 
44 ,55, 66].
 
Example 2:
Input:
L = 5
R = 40
Output:
8
Explanation:
Number with the same
first and last digits
in the given range is:
[5, 6, 7, 8, 9, 11, 
22, 33, 44].
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function numbersInRange() which takes integers L and R and returns the count of numbers if L>R return 0.
 
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= L,R <= 10^{8}

Source: geeksforgeeks | Difficulty: EASY
Tags: Number theory

Mathematics

Starter code: #User function Template for python3
class Solution:
    def numbersInRange (self,L,R):
        # code here 

Solutions:
class Solution:

	def numbersInRange(self, L, R):
		if L > R:
			return 0
		else:
			count = 0
			for i in range(L, R + 1):
				if str(i)[0] == str(i)[-1]:
					count += 1
			return count


class Solution:

	def numbersInRange(self, L, R):
		i = 0
		for t in range(L, R + 1):
			o = str(t)
			if o[0] == o[-1]:
				i += 1
		return i


class Solution:

	def numbersInRange(self, L, R):
		if L > R:
			return 0
		L -= 1
		s1 = str(L)
		s2 = str(R)
		z1 = int(L / 10)
		z2 = int(R / 10)
		if L >= 10:
			z1 += 9
		if R >= 10:
			z2 += 9
		if L < 10:
			z1 = L
		if s1[0] > s1[-1]:
			z1 -= 1
		if s2[0] > s2[-1]:
			z2 -= 1
		return z2 - z1


class Solution:

	def numbersInRange(self, L, R):
		s = 0
		for i in range(L, R + 1):
			if str(i)[0] == str(i)[-1]:
				s += 1
		return s


class Solution:

	def numbersInRange(self, L, R):
		i = L
		k = 0
		while i <= R:
			s = str(i)
			l = len(s) - 1
			l = 10 ** l
			a = int(i / l)
			b = int(s[-1])
			i = i + 1
			if a == b:
				k = k + 1
		return k


class Solution:

	def numbersInRange(self, L, R):
		c = 0
		for i in range(L, R + 1):
			k = str(i)
			if k[0] == k[-1]:
				c += 1
		return c


class Solution:

	def numbersInRange(self, L, R):
		ans = []
		for i in range(L, R + 1):
			if i < 10 or str(i)[0] == str(i)[-1]:
				ans.append(i)
		return len(ans)


class Solution:

	def numbersInRange(self, L, R):
		count = 0
		for i in range(L, R + 1):
			left = str(i)[0]
			right = str(i)[len(str(i)) - 1]
			if left == right:
				count += 1
		return count


class Solution:

	def numbersInRange(self, L, R):
		x = []
		for i in range(L, R + 1):
			a = str(i)
			if a[0] == a[-1]:
				x.append(i)
		return len(x)


class Solution:

	def numbersInRange(self, L, R):
		count = 0
		if L > R:
			return 0
		for i in range(L, R + 1):
			if len(str(i)) == 1:
				count += 1
			elif str(i)[0] == str(i)[-1]:
				count += 1
		return count
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(L, R) = map(int, input().strip().split(' '))
		ob = Solution()
		print(ob.numbersInRange(L, R))


class Solution:

	def numbersInRange(self, L, R):
		count = 0
		for i in range(L, R + 1):
			if i < 10:
				count += 1
			else:
				c = str(i)
				if int(c[0]) == int(c[-1]):
					count += 1
		return count
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(L, R) = map(int, input().strip().split(' '))
		ob = Solution()
		print(ob.numbersInRange(L, R))


class Solution:

	def numbersInRange(self, L, R):
		cnt = 0
		for i in range(L, R + 1):
			lis = str(i)
			if lis[0] == lis[-1]:
				cnt += 1
		return cnt


class Solution:

	def numbersInRange(self, L, R):
		cnt = 0
		for i in range(L, R + 1):
			lis = [int(x) for x in str(i)]
			n = len(lis)
			if lis[0] == lis[n - 1]:
				cnt += 1
		return cnt


class Solution:

	def numbersInRange(self, L, R):
		if L > R:
			return 0

		def check(n):
			while n >= 10:
				n //= 10
			return n

		def solve(n):
			if n < 10:
				return n
			ct = n // 10
			ans = ct + 9
			fd = check(n)
			ld = n % 10
			if ld < fd:
				ans -= 1
			return ans
		if L == 1:
			return solve(R)
		return solve(R) - solve(L - 1)


class Solution:

	def numbersInRange(self, L, R):
		kek = 0
		for k in range(L, R + 1):
			l = str(k)
			if l[0] == l[-1]:
				kek += 1
		return kek


class Solution:

	def numbersInRange(self, L, R):
		l = []
		for i in range(L, R + 1):
			if len(str(i)) == 1:
				l.append(i)
			elif str(i)[0] == str(i)[-1]:
				l.append(i)
		return len(l)


class Solution:

	def numbersInRange(self, L, R):
		c = 0
		for j in range(L, R + 1):
			if str(j)[0] == str(j)[-1]:
				c += 1
		return c


class Solution:

	def numbersInRange(self, L, R):
		if L > R:
			return 0
		n = R // 10 - L // 10 - 1
		sl = str(L / 10)
		sr = str(R / 10)
		if L != 0 and sl[-1] <= sl[0]:
			n += 1
		if sr[-1] >= sr[0]:
			n += 1
		if L < 10:
			n += 10 - L
		return n


class Solution:

	def cal(self, m):
		res = 0
		if m < 10:
			res = res + m
		else:
			rmodify = m // 10
			res = res + rmodify + 8
			m = str(m)
			if ord(m[0]) <= ord(m[len(m) - 1]):
				res = res + 1
		return res

	def numbersInRange(self, L, R):
		if L > R:
			return 0
		x = Solution().cal(L - 1)
		y = Solution().cal(R)
		return abs(y - x)


class Solution:

	def numbersInRange(self, L, R):
		ctr = 0
		for i in range(L, R + 1):
			txt = str(i)
			x = txt.endswith(txt[0])
			if x == True:
				ctr += 1
		return ctr


class Solution:

	def numbersInRange(self, L, R):
		val = ''
		ct = 0
		for i in range(L, R + 1):
			val = str(i)
			if val[0] == val[-1]:
				ct += 1
		return ct


class Solution:

	def numbersInRange(self, L, R):
		s = 0
		for i in range(L, R + 1):
			if i < 10:
				s += 1
			else:
				k = str(i)
				if k[0] == k[len(k) - 1]:
					s += 1
		return s