Maximum length substring with highest frequency in a string | C # 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
using System ; using System . Collections . Generic ; class GFG { static string MaxFreq ( string str ) { int n = str . Length ; Dictionary < string , int > m = new Dictionary < string , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { string sp = " " ; for ( int j = i ; j < n ; j ++ ) { sp += str [ j ] ; if ( m . ContainsKey ( sp ) ) { m [ sp ] ++ ; } else { m [ sp ] = 1 ; } } } int maxi = 0 ; string s = " " ; foreach ( KeyValuePair < string , int > i in m ) { if ( i . Value > maxi ) { maxi = i . Value ; s = i . Key ; } else if ( i . Value == maxi ) { string ss = i . Key ; if ( ss . Length > s . Length ) s = ss ; } } return s ; } public static void Main ( string [ ] args ) { string str = " ababecdecd " ; Console . Write ( MaxFreq ( str ) ) ; } }