Largest number not exceeding N that does not contain any of the digits of S | Function to obtain the largest number not exceeding num which does not contain any digits present in S ; Stores digits of S ; Traverse the string S ; Set occurrence as true ; Traverse the string n ; All digits of num are not present in string s ; Largest Digit not present in the string s ; Set all digits from positions in + 1 to n - 1 as LargestDig ; Counting leading zeroes ; Removing leading zeroes ; If the string becomes null ; Return the largest number ; Driver code
def greatestReducedNumber ( num , s ) : NEW_LINE INDENT vis_s = [ False ] * 10 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE vis_s [ ( ord ) ( s [ i ] ) - 48 ] = True NEW_LINE n = len ( num ) NEW_LINE In = - 1 NEW_LINE for i in range ( n ) : NEW_LINE if ( vis_s [ ord ( num [ i ] ) - ord ( '0' ) ] ) : NEW_LINE INDENT In = i NEW_LINE break NEW_LINE DEDENT if ( In == - 1 ) : NEW_LINE return num NEW_LINE for dig in range ( ord ( num [ In ] ) , ord ( '0' ) - 1 , - 1 ) : NEW_LINE if ( vis_s [ dig - ord ( '0' ) ] == False ) : NEW_LINE INDENT num = num [ 0 : In ] + chr ( dig ) + num [ In + 1 : n - In - 1 ] NEW_LINE break NEW_LINE DEDENT LargestDig = '0' NEW_LINE for dig in range ( ord ( '9' ) , ord ( '0' ) - 1 , - 1 ) : NEW_LINE if ( vis_s [ dig - ord ( '0' ) ] == False ) : NEW_LINE INDENT LargestDig = dig NEW_LINE break NEW_LINE DEDENT for i in range ( In + 1 , n ) : NEW_LINE num = num [ 0 : i ] + chr ( LargestDig ) NEW_LINE Count = 0 NEW_LINE for i in range ( n ) : NEW_LINE if ( num [ i ] == '0' ) : NEW_LINE INDENT Count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT num = num [ Count : n ] NEW_LINE if ( int ( len ( num ) ) == 0 ) : NEW_LINE return "0" NEW_LINE return num NEW_LINE DEDENT N = "12345" NEW_LINE S = "23" NEW_LINE print ( greatestReducedNumber ( N , S ) ) NEW_LINE