给定字符串S ,任务是找到具有最大可能长度的字符串S的前缀,以使该前缀中每个字符的频率最多为S中具有最小频率的字符数。
例子:
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.
方法:
- 初始化哈希图以存储字符的频率。
- 遍历字符串并增加哈希图中字符的频率。
- 查找字符串出现的最少字符,以及出现频率最小的此类字符的计数。
- 初始化另一个哈希图,以存储可能的前缀字符串的字符的频率。
- 最后,从头开始遍历字符串,并增加字符计数,直到任何字符的频率不大于最小频率的计数。
下面是上述方法的实现:
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
#include
using namespace std;
// Function to find the maximum
// possible prefix of the s
void MaxPrefix(string s)
{
// Hash map to store the frequency
// of the characters in the s
map Dict;
// Iterate over the s to find
// the occurence of each Character
for(char i : s)
{
Dict[i]++;
}
int minfrequency = INT_MAX;
// Minimum frequency of the Characters
for(auto x : Dict)
{
minfrequency = min(minfrequency, x.second);
}
int countminFrequency = 0;
// Loop to find the count of minimum
// frequency in the hash-map
for(auto x: Dict)
{
if (x.second == minfrequency)
countminFrequency += 1;
}
map mapper;
int indi = 0;
// Loop to find the maximum possible
// length of the prefix in the s
for(char i: s)
{
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.
cout << (s.substr(0, indi));
}
// Driver code
int main()
{
// s is initialize.
string str = "aabcdaab";
// str is passed in
// MaxPrefix function.
MaxPrefix(str);
}
// This code is contributed by mohit kumar 29
Java
// Java implementation to find the prefix
// of the s such that occurrence of each
// character is atmost the count of minimum
// frequency in the s
import java.util.*;
import java.lang.*;
import java.io.*;
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
Map Dict = new HashMap<>();
// Iterate over the s to find
// the occurence of each Character
for(char i : s.toCharArray())
{
Dict.put(i, Dict.getOrDefault(i, 0) + 1);
}
int minfrequency = Integer.MAX_VALUE;
// Minimum frequency of the Characters
for(Integer x: Dict.values())
{
minfrequency = Math.min(minfrequency, x);
}
int countminFrequency = 0;
// Loop to find the count of minimum
// frequency in the hash-map
for(Map.Entry x: Dict.entrySet())
{
if (x.getValue() == minfrequency)
countminFrequency += 1;
}
Map mapper = new HashMap<>();
int indi = 0;
// Loop to find the maximum possible
// length of the prefix in the s
for(char i: s.toCharArray())
{
mapper.put(i, mapper.getOrDefault(i, 0) + 1);
// Condition to check if the frequency
// is greater than minimum possible freq
if (mapper.get(i) > countminFrequency)
break;
indi += 1;
}
// maxprefix s and its length.
System.out.println(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 offbeat
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)
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 Dict = new Dictionary();
// 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 mapper = new Dictionary();
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
输出:
aabcd
性能分析:
- 时间复杂度:在上述方法中,有一个循环来查找字符串中每个字符的频率,在最坏的情况下需要O(N)时间。因此,该方法的时间复杂度将为O(N) 。
- 空间复杂度:在上述方法中,有多余的空间用于存储字符的频率。因此,上述方法的空间复杂度将为O(N)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。