Maximum length substring with highest frequency in a string | Java program to find maximum occurred subString of 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
import java . util . * ; class GFG { static String MaxFreq ( String str ) { int n = str . length ( ) ; Map < String , Integer > mp = new HashMap < String , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { String s = " " ; for ( int j = i ; j < n ; j ++ ) { s += str . charAt ( j ) ; if ( mp . containsKey ( s ) ) { mp . put ( s , mp . get ( s ) + 1 ) ; } else { mp . put ( s , 1 ) ; } } } int maxi = 0 ; String s = " " ; for ( Map . Entry < String , Integer > i : mp . entrySet ( ) ) { if ( i . getValue ( ) > maxi ) { maxi = i . getValue ( ) ; s = i . getKey ( ) ; } else if ( i . getValue ( ) == maxi ) { String ss = i . getKey ( ) ; if ( ss . length ( ) > s . length ( ) ) s = ss ; } } return s ; } public static void main ( String [ ] args ) { String str = " ababecdecd " ; System . out . print ( MaxFreq ( str ) ) ; } }