Given a string **S**, the task is to find the prefix of string **S** with the maximum possible length such that frequency of each character in the prefix is at most the number of characters in S with minimum frequency.

**Examples:**

Input:S = ‘aabcdaab’Output:aabcdExplanation:

Frequency of characters in the given string –

{a: 4, b: 2, c: 1, d: 1}

Minimum frequency in 1 and the count of minimum frequency is 2,

So frequency of each character in the prefix can be at most 2.

Input:S = ‘aaabc’Output:aaExplanation:

Frequency of characters in the given string –

{a: 3, b: 1, c: 1}

Minimum frequency in 1 and the count of minimum frequency is 2,

So frequency of each character in the prefix can be at most 2.

**Approach:**

- Initialize a hash-map to store the frequency of the characters.
- Iterate over the string and increment the frequency of the character in the hash-map.
- Find the minimum occurred character in the string and the count of such characters whose frequency is minimum.
- Initialize another hash-map to store the frequency of the characters of the possible prefix string.
- Finally, Iterate over the string from start and increment the count of the characters until the frequency of any characters is not greater than the count of the minimum frequency.

Below is the implementation of the above approach:

## Python3

`# Python3 implementation to find the` `# prefix of the string such that ` `# occurrence of each character is` `# atmost the count of minimum ` `# frequency in the string` `# Function to find the maximum` `# possible prefix of the string` `def` `MaxPrefix(string):` ` ` ` ` `# Hash map to store the frequency` ` ` `# of the characters in the string` ` ` `Dict` `=` `{}` ` ` `maxprefix ` `=` `0` ` ` ` ` `# Iterate over the string to find` ` ` `# the occurence of each Character` ` ` `for` `i ` `in` `string:` ` ` `Dict` `[i] ` `=` `Dict` `.get(i, ` `0` `) ` `+` `1` ` ` ` ` `# Minimum frequency of the Characters` ` ` `minfrequency ` `=` `min` `(` `Dict` `.values())` ` ` `countminFrequency ` `=` `0` ` ` ` ` `# Loop to find the count of minimum` ` ` `# frequency in the hash-map` ` ` `for` `x ` `in` `Dict` `:` ` ` `if` `(` `Dict` `[x] ` `=` `=` `minfrequency):` ` ` `countminFrequency ` `+` `=` `1` ` ` ` ` `mapper ` `=` `{}` ` ` `indi ` `=` `0` ` ` ` ` `# Loop to find the maximum possible ` ` ` `# length of the prefix in the string ` ` ` `for` `i ` `in` `string:` ` ` `mapper[i] ` `=` `mapper.get(i, ` `0` `) ` `+` `1` ` ` ` ` `# Condition to check if the frequency` ` ` `# is greater than minimum possible freq` ` ` `if` `(mapper[i] > countminFrequency):` ` ` `break` ` ` `indi ` `+` `=` `1` ` ` ` ` `# maxprefix string and its length.` ` ` `print` `(string[:indi])` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `# String is initialize.` ` ` `str` `=` `'aabcdaab'` ` ` `# str is passed in MaxPrefix function.` ` ` `MaxPrefix(` `str` `)` |

*chevron_right*

*filter_none*

## C#

`// 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` `using` `System;` `using` `System.Collections;` `using` `System.Collections.Generic;` `using` `System.Linq;` `class` `GFG{` ` ` `// Function to find the maximum` `// possible prefix of the s` `static` `void` `MaxPrefix(` `string` `s)` `{ ` ` ` ` ` `// Hash map to store the frequency` ` ` `// of the characters in the s` ` ` `Dictionary<` `char` `,` ` ` `int` `> Dict = ` `new` `Dictionary<` `char` `,` ` ` `int` `>();` ` ` `// Iterate over the s to find` ` ` `// the occurence of each Character` ` ` `foreach` `(` `char` `i ` `in` `s)` ` ` `{` ` ` `if` `(Dict.ContainsKey(i))` ` ` `{` ` ` `Dict[i]++;` ` ` `}` ` ` `else` ` ` `{` ` ` `Dict[i] = 1;` ` ` `}` ` ` `}` ` ` ` ` `int` `minfrequency = Int32.MaxValue;` ` ` ` ` `// Minimum frequency of the Characters` ` ` `foreach` `(` `int` `x ` `in` `Dict.Values.ToList())` ` ` `{` ` ` `minfrequency = Math.Min(minfrequency, x); ` ` ` `}` ` ` ` ` `int` `countminFrequency = 0;` ` ` ` ` `// Loop to find the count of minimum` ` ` `// frequency in the hash-map` ` ` `foreach` `(` `char` `x ` `in` `Dict.Keys.ToList())` ` ` `{` ` ` `if` `(Dict[x] == minfrequency)` ` ` `countminFrequency += 1;` ` ` `}` ` ` ` ` `Dictionary<` `char` `,` ` ` `int` `> mapper = ` `new` `Dictionary<` `char` `,` ` ` `int` `>(); ` ` ` `int` `indi = 0;` ` ` ` ` `// Loop to find the maximum possible ` ` ` `// length of the prefix in the s` ` ` `foreach` `(` `char` `i ` `in` `s)` ` ` `{` ` ` `if` `(mapper.ContainsKey(i))` ` ` `{` ` ` `mapper[i]++;` ` ` `}` ` ` `else` ` ` `{` ` ` `mapper[i] = 1;` ` ` `}` ` ` ` ` `// Condition to check if the frequency` ` ` `// is greater than minimum possible freq` ` ` `if` `(mapper[i] > countminFrequency)` ` ` `break` `;` ` ` ` ` `indi += 1;` ` ` `}` ` ` ` ` `// maxprefix s and its length.` ` ` `Console.Write(s.Substring(0, indi));` `}` `// Driver Code` `public` `static` `void` `Main(` `string` `[] args)` `{` ` ` ` ` `// s is initialize.` ` ` `string` `str = ` `"aabcdaab"` `;` ` ` ` ` `// str is passed in ` ` ` `// MaxPrefix function.` ` ` `MaxPrefix(str);` `}` `}` `// This code is contributed by rutvik_56` |

*chevron_right*

*filter_none*

**Output:**

aabcd

**Performance Analysis:**

**Time Complexity:**In the above-given approach, there is one loop to find the frequency of each character in the string which takes O(N) time in the worst case. Therefore, the time complexity for this approach will be**O(N)**.**Space Complexity:**In the above-given approach, there is extra space used to store the frequency of characters. Therefore, the space complexity for the above approach will be**O(N)**

## Recommended Posts:

- Longest suffix such that occurrence of each character is less than N after deleting atmost K characters
- Minimum addition/removal of characters to be done to make frequency of each character prime
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Longest substring with atmost K characters from the given set of characters
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Maximum sum increasing subsequence from a prefix and a given element after prefix is must
- Longest Common Prefix using Character by Character Matching
- Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Minimum characters to be replaced to make frequency of all characters same
- Python | Delete elements with frequency atmost K
- Python - Keys Frequency with Value atmost K
- Minimize length of prefix of string S containing all characters of another string T
- Print number of words, vowels and frequency of each character
- Maximum difference between frequency of two elements such that element having greater frequency is also greater
- Count all sub-strings with weight of characters atmost K
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Length of the longest subsegment which is UpDown after inserting atmost one integer
- Count of subsequences of length atmost K containing distinct prime elements

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.