Max count of N using digits of M such that 2 and 5 , and , 6 and 9 can be treated as same respectively | Function to find the count of numbers that can be formed using the given digits in the string ; Store the frequency of digits from the given string M ; Store length of the string M ; Loop to traverse the string ; Replace 5 with 2 ; Replace 9 with 6 ; Get the int form of the current character in the string ; Insert in the map ; Store all the digits of the required number N ; Loop to get all the digits from the number N ; Get the last digit as the remainder ; Replace 5 with 2 ; Replace 9 with 6 ; Insert the remainders in the rems map ; Store the resultant count ; Iterate through the rems map ; Get the key which is a digit from the number N to be formed ; If not present in the string M , number N that cannot be formed ; Divide the frequency of the digit from the string M with the frequency of the current remainder ; Choose the minimum ; Return the maximum count ; Driver Code
function solve ( n , str ) { let mymap = new Map ( ) ; let len = str . length ; for ( let i = 0 ; i < len ; i ++ ) { let c = str . charAt ( i ) ; if ( c == " " ) c = " " ; else if ( c == " " ) c = " " ; let c_int = parseInt ( c ) ; if ( mymap . has ( c_int ) ) mymap . set ( c_int , mymap . get ( c_int ) + 1 ) ; else mymap . set ( c_int , 1 ) ; } let rems = new Map ( ) ; while ( n > 0 ) { let rem = n % 10 ; if ( rem == 5 ) rem = 2 ; if ( rem == 9 ) rem = 6 ; if ( rems . has ( rem ) ) rems . set ( rem , rems . get ( rem ) + 1 ) ; else rems . set ( rem , 1 ) ; n = Math . floor ( n / 10 ) ; } let cnt = Number . MAX_SAFE_INTEGER ; for ( let ele of rems ) { let key = ele [ 0 ] ; if ( ! mymap . has ( key ) ) return 0 ; let temp = mymap . get ( key ) / ele [ 1 ] ; cnt = Math . min ( cnt , temp ) ; } return cnt ; } let N = 56 ; let M = " " ; document . write ( solve ( N , M ) ) ;