Count of subarrays of an Array having all unique digits | Function to check whether the subarray has all unique digits ; Storing all digits occurred ; Traversing all the numbers of v ; Storing all digits of v [ i ] ; Checking whether digits of v [ i ] have already occurred ; Inserting digits of v [ i ] in the set ; Function to count the number subarray with all digits unique ; Traverse through all the subarrays ; To store elements of this subarray ; Generate all subarray and store it in vector ; Check whether this subarray has all digits unique ; Increase the count ; Return the count ; Driver code
def check ( v ) : NEW_LINE INDENT digits = set ( ) NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT d = set ( ) NEW_LINE while ( v [ i ] != 0 ) : NEW_LINE INDENT d . add ( v [ i ] % 10 ) NEW_LINE v [ i ] //= 10 NEW_LINE DEDENT for it in d : NEW_LINE INDENT if it in digits : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT for it in d : NEW_LINE INDENT digits . add ( it ) NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def numberOfSubarrays ( a , n ) : NEW_LINE INDENT answer = 0 NEW_LINE for i in range ( 1 , 1 << n ) : NEW_LINE INDENT temp = [ ] NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( i & ( 1 << j ) ) : NEW_LINE INDENT temp . append ( a [ j ] ) NEW_LINE DEDENT DEDENT if ( check ( temp ) ) : NEW_LINE INDENT answer += 1 NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE A = [ 1 , 12 , 23 , 34 ] NEW_LINE print ( numberOfSubarrays ( A , N ) ) NEW_LINE DEDENT