Count array elements whose all distinct digits appear in K | Function to check a digit occurs in the digit of K or not ; Iterate over all possible digits of K ; If current digit equal to digit ; Update K ; Function to find the count of array elements whose distinct digits are a subset of digits of K ; Stores count of array elements whose distinct digits are subset of digits of K ; Traverse the array , [ ] arr ; Stores the current element ; Check if all the digits arr [ i ] is a subset of the digits of K or not ; Iterate over all possible digits of arr [ i ] ; Stores current digit ; If current digit does not appear in K ; Update flag ; Update no ; If all the digits arr [ i ] appear in K ; Update count ; Finally print count ; Driver Code
def isValidDigit ( digit , K ) : NEW_LINE INDENT while ( K != 0 ) : NEW_LINE INDENT if ( K % 10 == digit ) : NEW_LINE INDENT return True NEW_LINE DEDENT K = K // 10 NEW_LINE DEDENT return False NEW_LINE DEDENT def noOfValidNumbers ( K , arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT no = arr [ i ] NEW_LINE flag = True NEW_LINE while ( no != 0 ) : NEW_LINE INDENT digit = no % 10 NEW_LINE if ( not isValidDigit ( digit , K ) ) : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT no = no // 10 NEW_LINE DEDENT if ( flag == True ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT K = 12 NEW_LINE arr = [ 1 , 12 , 1222 , 13 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( noOfValidNumbers ( K , arr , n ) ) NEW_LINE DEDENT