Given two numbers N and P, the task is to check if unit digit of N and N^{P}^{ }are the same or not. 
Example 1:
Input: N = "2", P = 1
Output: 1
Explanation: Unit digit of 2^{1} is 2, 
which is equals to 2
Example 2:
Input: N = "4", P = 2
Output: 0
Explanation: Unit digit of 4^{2} is 6,
which is not equals to 4 
Your Task:
You don't need to read input or print anything. Your task is to complete the function unitDigit() which takes a string N and an integer P as inputs and returns the boolean value. 
Expected Time Complexity: O(|P|).
Expected Auxiliary Space: O(1).
Constraints:
1 <= |N| <= 50
1 <= P <= 1000

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Mathematics

Starter code: #User function Template for python3

class Solution:
    def unitDigit(self, N, P):
        # code here

Solutions:
class Solution:

	def unitDigit(self, N, P):
		return int(N[-1] == str(int(N) ** P % 10))


class Solution:

	def unitDigit(self, N, P):
		a = int(N)
		b = int(P)
		c = pow(a, b)
		s = c % 10
		l = a % 10
		if l == s:
			return 1
		else:
			return 0


class Solution:

	def unitDigit(self, N, P):
		ans = int(N) ** int(P) % 10
		if ans == int(N) % 10:
			return 1
		else:
			return 0


class Solution:

	def unitDigit(self, N, P):
		N = int(str(N)[-1])
		P = int(P)
		U = int(str(N ** P)[-1])
		return 1 if N == U else 0


class Solution:

	def unitDigit(self, N, P):
		a = int(N[-1]) ** P
		if N[-1] == str(a)[-1]:
			return True
		return False


class Solution:

	def unitDigit(self, N, P):
		unit = N[-1]
		temp = int(N) ** P
		last = temp % 10
		if int(unit) == last:
			return 1
		return 0


class Solution:

	def unitDigit(self, a, p):
		if p == 0:
			if p == 1:
				return True
			return False
		if a % 10 == 1 or a % 10 == 0 or a % 10 == 6 or (a % 10 == 5):
			return True
		s = {2: [6, 2, 4, 8], 7: [1, 7, 9, 3], 8: [6, 8, 4, 2], 3: [1, 3, 9, 7]}
		if a % 10 in s:
			return a % 10 == s[a % 10][p % 4]
		m = {9: [1, 9], 4: [6, 4]}
		return a % 10 == m[a % 10][p % 2]


class Solution:

	def unitDigit(self, N, P):
		n = int(N) % 10
		s = int(N) ** int(P)
		if s % 10 == n:
			return 1
		else:
			return 0


class Solution:

	def unitDigit(self, N, P):
		sum = int(N)
		j = pow(sum, P)
		j = j % 10
		if j == sum or j == sum % 10:
			return 1
		return 0