Count of numbers in the range [ L , R ] which satisfy the given conditions | Python3 implementation of the approach ; Maximum possible valid number ; To store all the required number from the range [ 1 , MAX ] ; Function that returns true if x satisfies the given conditions ; To store the digits of x ; If current digit appears more than once ; If current digit is greater than 5 ; Put the digit in the map ; Function to generate all the required numbers in the range [ 1 , MAX ] ; Insert first 5 valid numbers ; Inserting 0 externally because 0 cannot be the leading digit in any number ; If x satisfies the given conditions ; Cannot append anymore digit as adding a digit will repeat one of the already present digits ; Append all the valid digits one by one and append the new generated number to the queue ; Append the digit ; Push the newly generated number to the queue ; Function to copmpare two strings which represent a numerical value ; Function to return the count of valid numbers in the range [ l , r ] ; Generate all the valid numbers in the range [ 1 , MAX ] ; To store the count of numbers in the range [ l , r ] ; For every valid number in the range [ 1 , MAX ] ; If current number is within the required range ; If number is equal to either l or r ; Driver code
from collections import deque NEW_LINE MAX = 543210 NEW_LINE ans = [ ] NEW_LINE def isValidNum ( x ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE for i in range ( len ( x ) ) : NEW_LINE INDENT if ( ord ( x [ i ] ) - ord ( '0' ) in mp . keys ( ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT elif ( ord ( x [ i ] ) - ord ( '0' ) > 5 ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT mp [ ord ( x [ i ] ) - ord ( '0' ) ] = 1 NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def generate ( ) : NEW_LINE INDENT q = deque ( ) NEW_LINE q . append ( "1" ) NEW_LINE q . append ( "2" ) NEW_LINE q . append ( "3" ) NEW_LINE q . append ( "4" ) NEW_LINE q . append ( "5" ) NEW_LINE flag = True NEW_LINE ans . append ( "0" ) NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT x = q . popleft ( ) NEW_LINE if ( isValidNum ( x ) ) : NEW_LINE INDENT ans . append ( x ) NEW_LINE DEDENT if ( len ( x ) == 6 ) : NEW_LINE INDENT continue NEW_LINE DEDENT for i in range ( 6 ) : NEW_LINE INDENT z = str ( i ) NEW_LINE temp = x + z NEW_LINE q . append ( temp ) NEW_LINE DEDENT DEDENT DEDENT def comp ( a , b ) : NEW_LINE INDENT if ( len ( a ) == len ( b ) ) : NEW_LINE INDENT if a < b : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return len ( a ) < len ( b ) NEW_LINE DEDENT DEDENT def findcount ( l , r ) : NEW_LINE INDENT generate ( ) NEW_LINE count = 0 NEW_LINE for i in range ( len ( ans ) ) : NEW_LINE INDENT a = ans [ i ] NEW_LINE if ( comp ( l , a ) and comp ( a , r ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT elif ( a == l or a == r ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT l = "1" NEW_LINE r = "1000" NEW_LINE print ( findcount ( l , r ) ) NEW_LINE