📌  相关文章
📜  最大长度前缀,使得每个字符的频率最多是频率最低的字符数

📅  最后修改于: 2021-09-06 06:47:21             🧑  作者: Mango

给定一个字符串S ,任务是找到字符串S的最大可能长度的前缀,使得前缀中每个字符的频率至多是 S 中频率最低的字符数。

例子:

方法:

  • 初始化一个哈希映射来存储字符的频率。
  • 迭代字符串并增加哈希映射中字符的频率。
  • 查找字符串中的最小字符发生和这样的字符,其频率是最低的计数。
  • 初始化另一个 hash-map 来存储可能的前缀字符串的字符的频率。
  • 最后,从头开始迭代字符串并增加字符的计数,直到任何字符的频率不大于最小频率的计数。

下面是上述方法的实现:

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 Classes Live