Count of numbers in the range [ L , R ] which satisfy the given conditions | 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 push 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
let MAX = 543210 ; let ans = [ ] ; function isValidNum ( x ) { let mp = new Map ( ) ; for ( let i = 0 ; i < x . length ; i ++ ) { if ( mp . has ( x [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) { return false ; } else if ( x [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) > 5 ) { return false ; } else { mp . set ( x [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) , 1 ) ; } } return true ; } function generate ( ) { let q = [ ] ; q . push ( " " ) ; q . push ( " " ) ; q . push ( " " ) ; q . push ( " " ) ; q . push ( " " ) ; let flag = true ; ans . push ( " " ) ; while ( q . length != 0 ) { let x = q . shift ( ) ; if ( isValidNum ( x ) ) { ans . push ( x ) ; } if ( x . length == 6 ) continue ; for ( let i = 0 ; i <= 5 ; i ++ ) { let z = ( i ) . toString ( ) ; let temp = x + z ; q . push ( temp ) ; } } } function comp ( a , b ) { if ( a . length == b . length ) { return a < b ? true : false ; } else return a . length < b . length ; } function findcount ( l , r ) { generate ( ) ; let count = 0 ; for ( let i = 0 ; i < ans . length ; i ++ ) { let a = ans [ i ] ; if ( comp ( l , a ) && comp ( a , r ) ) { count ++ ; } else if ( a == l a == r ) { count ++ ; } } return count ; } let l = " " , r = " " ; document . write ( findcount ( l , r ) ) ;