Count of numbers in the range [ L , R ] which satisfy the given conditions | Java 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 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
import java . util . * ; class GFG { static int MAX = 543210 ; static Vector < String > ans = new Vector < String > ( ) ; static boolean isValidNum ( String x ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < x . length ( ) ; i ++ ) { if ( mp . containsKey ( x . charAt ( i ) - '0' ) ) { return false ; } else if ( x . charAt ( i ) - '0' > 5 ) { return false ; } else { mp . put ( x . charAt ( i ) - '0' , 1 ) ; } } return true ; } static void generate ( ) { Queue < String > q = new LinkedList < String > ( ) ; q . add ( "1" ) ; q . add ( "2" ) ; q . add ( "3" ) ; q . add ( "4" ) ; q . add ( "5" ) ; boolean flag = true ; ans . add ( "0" ) ; while ( ! q . isEmpty ( ) ) { String x = q . peek ( ) ; q . remove ( ) ; if ( isValidNum ( x ) ) { ans . add ( x ) ; } if ( x . length ( ) == 6 ) continue ; for ( int i = 0 ; i <= 5 ; i ++ ) { String z = String . valueOf ( i ) ; String temp = x + z ; q . add ( temp ) ; } } } static boolean comp ( String a , String b ) { if ( a . length ( ) == b . length ( ) ) { int i = a . compareTo ( b ) ; return i < 0 ? true : false ; } else return a . length ( ) < b . length ( ) ; } static int findcount ( String l , String r ) { generate ( ) ; int count = 0 ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { String a = ans . get ( i ) ; if ( comp ( l , a ) && comp ( a , r ) ) { count ++ ; } else if ( a == l a == r ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { String l = "1" , r = "1000" ; System . out . println ( findcount ( l , r ) ) ; } }