Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency | Function to find the maximum possible prefix of the s ; Hash map to store the frequency of the characters in the s ; Iterate over the s to find the occurence of each Character ; Minimum frequency of the Characters ; Loop to find the count of minimum frequency in the hash - map ; Loop to find the maximum possible length of the prefix in the s ; Condition to check if the frequency is greater than minimum possible freq ; maxprefix s and its length . ; Driver Code s is initialize . ; str is passed in MaxPrefix function .
function MaxPrefix ( s ) { var Dict = { } ; for ( const i of s ) { if ( Dict . hasOwnProperty ( i ) ) { Dict [ i ] ++ ; } else { Dict [ i ] = 1 ; } } var minfrequency = 2147483647 ; for ( const [ key , value ] of Object . entries ( Dict ) ) { minfrequency = Math . min ( minfrequency , value ) ; } var countminFrequency = 0 ; for ( const [ key , value ] of Object . entries ( Dict ) ) { if ( Dict [ key ] === minfrequency ) countminFrequency += 1 ; } var mapper = { } ; var indi = 0 ; for ( const i of s ) { if ( mapper . hasOwnProperty ( i ) ) { mapper [ i ] ++ ; } else { mapper [ i ] = 1 ; } if ( mapper [ i ] > countminFrequency ) break ; indi += 1 ; } document . write ( s . substring ( 0 , indi ) ) ; } var str = " " ; MaxPrefix ( str ) ;