Check if the product of every contiguous subsequence is different or not in a number | Function that returns true if the product of every digit of a contiguous subsequence is distinct ; To store the given number as a string ; Append all the digits starting from the end ; Reverse the string to get the original number ; Store size of the string ; Set to store product of each contiguous subsequence ; Find product of every contiguous subsequence ; If current product already exists in the set ; Driver code
def productsDistinct ( N ) : NEW_LINE INDENT s = " " NEW_LINE while ( N ) : NEW_LINE INDENT s += chr ( N % 10 + ord ( '0' ) ) NEW_LINE N //= 10 NEW_LINE DEDENT s = s [ : : - 1 ] NEW_LINE sz = len ( s ) NEW_LINE se = [ ] NEW_LINE for i in range ( sz ) : NEW_LINE INDENT product = 1 NEW_LINE for j in range ( i , sz , 1 ) : NEW_LINE INDENT product *= ord ( s [ j ] ) - ord ( '0' ) NEW_LINE for p in range ( len ( se ) ) : NEW_LINE INDENT if se [ p ] == product : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT se . append ( product ) NEW_LINE DEDENT DEDENT DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2345 NEW_LINE if ( productsDistinct ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT