Total numbers with no repeated digits in a range | Java implementation of above idea ; Maximum ; Prefix Array ; Function to check if the given number has repeated digit or not ; Traversing through each digit ; if the digit is present more than once in the number ; return 0 if the number has repeated digit ; return 1 if the number has no repeated digit ; Function to pre calculate the Prefix array ; Traversing through the numbers from 2 to MAX ; Generating the Prefix array ; Calclute Function ; Answer ; Driver Code ; Pre - calculating the Prefix array . ; Calling the calculate function to find the total number of number which has no repeated digit
import java . util . * ; class GFG { static int MAX = 100 ; static Vector < Integer > Prefix = new Vector < > ( ) ; static int repeated_digit ( int n ) { HashSet < Integer > a = new HashSet < > ( ) ; int d ; while ( n != 0 ) { d = n % 10 ; if ( a . contains ( d ) ) return 0 ; a . add ( d ) ; n /= 10 ; } return 1 ; } static void pre_calculations ( ) { Prefix . add ( 0 ) ; Prefix . add ( repeated_digit ( 1 ) ) ; for ( int i = 2 ; i < MAX + 1 ; i ++ ) Prefix . add ( repeated_digit ( i ) + Prefix . elementAt ( i - 1 ) ) ; } static int calculate ( int L , int R ) { return Prefix . elementAt ( R ) - Prefix . elementAt ( L - 1 ) ; } public static void main ( String [ ] args ) { int L = 1 , R = 100 ; pre_calculations ( ) ; System . out . println ( calculate ( L , R ) ) ; } }