Total numbers with no repeated digits in a range | C # 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
using System ; using System . Collections . Generic ; class GFG { static int MAX = 100 ; static List < int > Prefix = new List < int > ( ) ; static int repeated_digit ( int n ) { HashSet < int > a = new HashSet < int > ( ) ; 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 [ i - 1 ] ) ; } static int calculate ( int L , int R ) { return Prefix [ R ] - Prefix [ L - 1 ] ; } public static void Main ( String [ ] args ) { int L = 1 , R = 100 ; pre_calculations ( ) ; Console . WriteLine ( calculate ( L , R ) ) ; } }