A n digit number has n*(n+1)/2 sub-numbers.  For example, all possible sub-numbers of 975 are 9 7 5 97 75 975. A number is called Lucky if all sub-numbers have different digit product.  Digit product of a number is product of its digits.   
For example, the number 23 is Lucky.  Sub-numbers of it are 2, 3 and 23 and digit products are 2, 3 and 6 respectively (All digit products are different). 975 is also lucky. Print 1 if given Number is Lucky else Print 0.
 
Example 1:
Input:
N = 324
Output:
1
Explanation:
All subsequences form different Digit
Products. So, Output is 1.
Example 2:
Input:
N = 323
Output:
0
Explanation:
Subsequences 32, and 23 form same
Digit Product, i.e, 6. So, Output is 0.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function isLuckyOrNot() which takes an Integer N as input and returns the answer.
 
Expected Time Complexity: O((Number of Digits)^{2})
Expected Auxiliary Space: O(Number of Digits)
 
Constraints:
1 <= N <= 10^{18}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Mathematics

Starter code: #User function Template for python3



class Solution:

    def isLuckyOrNot(self, N):

        # code here 

Solutions:
class Solution:

	def isLuckyOrNot(self, N):
		s = str(N)
		n = len(s)
		ss = set()
		for i in range(n):
			p = 1
			for j in range(i, n):
				p *= int(s[j])
				ss.add(p)
		if len(ss) == n * (n + 1) // 2:
			return 1
		return 0


class Solution:

	def isLuckyOrNot(self, N):
		num_list = []
		h = set()
		while N > 0:
			num = N % 10
			num_list.append(num)
			N = N // 10
		dp = []
		for j in range(len(num_list)):
			temp = []
			for i in range(len(num_list)):
				temp.append(num_list[i])
			dp.append(temp)
		for i in range(len(num_list)):
			dp[i][i] = num_list[i]
			if dp[i][i] in h:
				return 0
			h.add(dp[i][i])
		for i in range(len(num_list)):
			for j in range(i + 1, len(num_list)):
				dp[i][j] = dp[i][j - 1] * num_list[j]
				if dp[i][j] in h:
					return 0
				h.add(dp[i][j])
		return 1


import itertools

class Solution:

	def isLuckyOrNot(self, N):
		(dn, dl) = (N, [])
		while dn > 0:
			dl.append(dn % 10)
			dn = dn // 10
		res = []
		for i in range(1, len(dl) + 1):
			l = list(itertools.combinations(dl, i))
			for j in l:
				prod = 1
				for k in j:
					prod = prod * k
				if prod in res:
					return 0
				else:
					res.append(prod)
		return 1


class Solution:

	def isLuckyOrNot(self, N):
		s = str(N)
		l = list(s)
		l1 = []
		for i in range(len(s)):
			for j in range(len(s)):
				l1.append(l[i:j + 1])
		d = {}
		for i in l1:
			p = 1
			if i != []:
				for j in i:
					p *= int(j)
				if d == {}:
					d[p] = 1
				elif p not in d:
					d[p] = 1
				else:
					return 0
		return 1


class Solution:

	def isLuckyOrNot(self, N):
		num = str(N)
		d = {}
		for i in range(len(num)):
			for j in range(i + 1, len(num) + 1):
				product = 1
				for item in map(int, num[i:j]):
					product *= item
				if product in d:
					return 0
				else:
					d[product] = 1
		return 1


class Solution:

	def isLuckyOrNot(self, N):
		num = str(N)
		lst = []
		for i in range(len(num)):
			for j in range(i + 1, len(num) + 1):
				lst.append(num[i:j])
		res = []
		for item in lst:
			ans = 1
			for ele in item:
				ans *= int(ele)
			if ans in res:
				return 0
			res.append(ans)
		return 1


class Solution:

	def isLuckyOrNot(self, N):
		num = str(N)
		nums = []
		luck = set()
		for i in range(len(num)):
			for j in range(i + 1, len(num) + 1):
				nums.append(num[i:j])
		for n in nums:
			prod = 1
			for digit in n:
				prod *= int(digit)
			if prod in luck:
				return 0
			luck.add(prod)
		return 1


from itertools import combinations

class Solution:

	def isLuckyOrNot(self, N):
		N = str(N)

		def helper(m):
			a = 1
			for i in m:
				a *= int(i)
			return a
		x = [i for i in range(len(str(N)) + 1)]
		y = []
		count = 0
		for (i, J) in combinations(x, 2):
			b = N[i:J]
			y.append(helper(b))
		if len(y) != len(set(y)):
			return 0
		return 1


class Solution:

	def isLuckyOrNot(self, N):
		inp = str(N)
		ln = len(inp)
		substr = []
		dic = {}
		for i in range(ln):
			for j in range(i + 1, ln + 1):
				substr.append(inp[i:j])
		for i in range(len(substr)):
			temp = 1
			for j in range(len(substr[i])):
				temp *= int(substr[i][j])
			substr[i] = temp
		for elem in substr:
			if dic.get(elem) != None:
				return 0
			dic[elem] = 'Present'
		return 1


class Solution:

	def isLuckyOrNot(self, N):
		k = str(N)
		hash = {}
		if len(k) == 1:
			return 1
		for i in k:
			if i == '1' or i == '0':
				return 0
			else:
				hash[i] = hash.get(i, 0) + 1
				if hash[i] > 1:
					return 0
		return 1


def prod(s):
	ans = 1
	for i in s:
		ans *= int(i)
	return ans

class Solution:

	def isLuckyOrNot(self, N):
		s = str(N)
		lst = [s[i:j] for i in range(len(s)) for j in range(i + 1, len(s) + 1)]
		se = set()
		for i in lst:
			se.add(prod(i))
		if len(se) == len(s) * (len(s) + 1) // 2:
			return 1
		else:
			return 0


class Solution:

	def isLuckyOrNot(self, N):
		if N < 10:
			return 1
		N = sorted(str(N))
		for j in range(0, len(N) - 1):
			if N[j] == N[j + 1] or N[j] == '1':
				return 0
		return 1


class Solution:

	def isLuckyOrNot(self, N):
		x = str(N)
		leng = len(str(N))
		res = []
		for i in range(leng):
			for j in range(i, leng):
				res.append(x[i:j + 1])
		d = {}
		for e in res:
			m = 1
			for i in range(len(e)):
				m *= int(e[i])
			if m not in d:
				d[m] = 1
			else:
				return 0
		return 1


class Solution:

	def isLuckyOrNot(self, N):
		s = str(N)
		digit_products = set()
		sub_numbers = []
		for i in range(len(s)):
			sub_numbers.append(s[i])

		def digit_product(N_str, start, end):
			digit_product = 1
			while start <= end:
				digit_product *= int(N_str[start])
				start += 1
			return digit_product
		for i in range(len(s)):
			start = end = i
			while end < len(s):
				dp = digit_product(s, start, end)
				if dp not in digit_products:
					digit_products.add(dp)
				else:
					return 0
				end += 1
		return 1


class Solution:

	def isLuckyOrNot(self, N):

		def digitProduct(digits, start, end):
			pro = 1
			for i in range(start, end + 1):
				pro *= digits[i]
			return pro
		s = str(N)
		length = len(s)
		digits = [0] * length
		products = set()
		for i in range(0, length):
			digits[i] = int(s[i])
		for i in range(0, length):
			for j in range(i, length):
				val = digitProduct(digits, i, j)
				if val in products:
					return 0
				else:
					products.add(val)
		return 1


import math

class Solution:

	def multi(self, n):
		x = 1
		while n > 0:
			x = x * (n % 10)
			n = n // 10
		return x

	def isLuckyOrNot(self, N):
		from itertools import combinations
		y = str(N)
		z = [y[i:j] for (i, j) in combinations(range(len(y) + 1), r=2)]
		y = set()
		for i in z:
			if self.multi(int(i)) in y:
				return 0
				break
			else:
				y.add(self.multi(int(i)))
		else:
			return 1