Maximum length substring with highest frequency in a string | Function to return maximum occurred subString of a String ; Size of the String ; To store maximum frequency ; To store String which has maximum frequency ; Return subString which has maximum frequency ; Driver code ; Function call
function MaxFreq ( str ) { let n = str . length ; let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { let s = " " ; for ( let j = i ; j < n ; j ++ ) { s += str [ j ] ; if ( mp . has ( s ) ) { mp . set ( s , mp . get ( s ) + 1 ) ; } else { mp . set ( s , 1 ) ; } } } let maxi = 0 ; let s = " " ; for ( let [ key , value ] of mp . entries ( ) ) { if ( value > maxi ) { maxi = value ; s = key ; } else if ( value == maxi ) { let ss = key ; if ( ss . length > s . length ) s = ss ; } } return s ; } let str = " " ; document . write ( MaxFreq ( str ) ) ;