Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency | C # implementation to find the prefix of the s such that occurrence of each character is atmost the count of minimum frequency in the s ; 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 .
using System ; using System . Collections ; using System . Collections . Generic ; using System . Linq ; class GFG { static void MaxPrefix ( string s ) { Dictionary < char , int > Dict = new Dictionary < char , int > ( ) ; foreach ( char i in s ) { if ( Dict . ContainsKey ( i ) ) { Dict [ i ] ++ ; } else { Dict [ i ] = 1 ; } } int minfrequency = Int32 . MaxValue ; foreach ( int x in Dict . Values . ToList ( ) ) { minfrequency = Math . Min ( minfrequency , x ) ; } int countminFrequency = 0 ; foreach ( char x in Dict . Keys . ToList ( ) ) { if ( Dict [ x ] == minfrequency ) countminFrequency += 1 ; } Dictionary < char , int > mapper = new Dictionary < char , int > ( ) ; int indi = 0 ; foreach ( char i in s ) { if ( mapper . ContainsKey ( i ) ) { mapper [ i ] ++ ; } else { mapper [ i ] = 1 ; } if ( mapper [ i ] > countminFrequency ) break ; indi += 1 ; } Console . Write ( s . Substring ( 0 , indi ) ) ; } public static void Main ( string [ ] args ) { string str = " aabcdaab " ; MaxPrefix ( str ) ; } }