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: aabcd
Explanation:
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: aa
Explanation:
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:
C++
#include <bits/stdc++.h>
using namespace std;
void MaxPrefix(string s)
{
map< char , int > Dict;
for ( char i : s)
{
Dict[i]++;
}
int minfrequency = INT_MAX;
for ( auto x : Dict)
{
minfrequency = min(minfrequency, x.second);
}
int countminFrequency = 0;
for ( auto x: Dict)
{
if (x.second == minfrequency)
countminFrequency += 1;
}
map< char , int > mapper;
int indi = 0;
for ( char i: s)
{
mapper[i] += 1;
if (mapper[i] > countminFrequency)
break ;
indi += 1;
}
cout << (s.substr(0, indi));
}
int main()
{
string str = "aabcdaab" ;
MaxPrefix(str);
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG{
static void MaxPrefix(String s)
{
Map<Character,
Integer> Dict = new HashMap<>();
for ( char i : s.toCharArray())
{
Dict.put(i, Dict.getOrDefault(i, 0 ) + 1 );
}
int minfrequency = Integer.MAX_VALUE;
for (Integer x: Dict.values())
{
minfrequency = Math.min(minfrequency, x);
}
int countminFrequency = 0 ;
for (Map.Entry<Character,
Integer> x: Dict.entrySet())
{
if (x.getValue() == minfrequency)
countminFrequency += 1 ;
}
Map<Character,
Integer> mapper = new HashMap<>();
int indi = 0 ;
for ( char i: s.toCharArray())
{
mapper.put(i, mapper.getOrDefault(i, 0 ) + 1 );
if (mapper.get(i) > countminFrequency)
break ;
indi += 1 ;
}
System.out.println(s.substring( 0 , indi));
}
public static void main(String[] args)
{
String str = "aabcdaab" ;
MaxPrefix(str);
}
}
|
Python3
def MaxPrefix(string):
Dict = {}
maxprefix = 0
for i in string:
Dict [i] = Dict .get(i, 0 ) + 1
minfrequency = min ( Dict .values())
countminFrequency = 0
for x in Dict :
if ( Dict [x] = = minfrequency):
countminFrequency + = 1
mapper = {}
indi = 0
for i in string:
mapper[i] = mapper.get(i, 0 ) + 1
if (mapper[i] > countminFrequency):
break
indi + = 1
print (string[:indi])
if __name__ = = '__main__' :
str = 'aabcdaab'
MaxPrefix( str )
|
C#
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);
}
}
|
Javascript
<script>
function MaxPrefix(s) {
var Dict = {};
for (const i of s) {
if (Dict.hasOwnProperty(i)) {
Dict[i]++;
} else {
Dict[i] = 1;
}
}
var minfrequency = 2147483647;
for (const [key, value] of Object.entries(Dict)) {
minfrequency = Math.min(minfrequency, value);
}
var countminFrequency = 0;
for (const [key, value] of Object.entries(Dict)) {
if (Dict[key] === minfrequency) countminFrequency += 1;
}
var mapper = {};
var indi = 0;
for (const i of s) {
if (mapper.hasOwnProperty(i)) {
mapper[i]++;
} else {
mapper[i] = 1;
}
if (mapper[i] > countminFrequency) break ;
indi += 1;
}
document.write(s.substring(0, indi));
}
var str = "aabcdaab" ;
MaxPrefix(str);
</script>
|
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)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
02 Jun, 2022
Like Article
Save Article