📌  相关文章
📜  给定字符串中字符的最大重复频率

📅  最后修改于: 2021-10-27 07:28:42             🧑  作者: Mango

给定一个字符串S ,任务是找到给定字符串S 中字符的最大重复频率的计数。
例子

有效的方法:

  • 我们的想法是第一家专卖店在大小26的阵列由于字符串的所有字符的字符串的字符频率是其中26个小写英文字母,就可以字符的频率存储在大小26的阵列。
  • 创建一个哈希映射,用于存储字符的频率计数并返回出现次数最多的频率。

下面是上述方法的实现:

C++
// C++ implementation to find the
// maximum repeated frequency of
// characters in the given string
 
#include 
using namespace std;
 
// Function to find the maximum
// repeated frequency of the
// characters in the given string
void findMaxFrequency(string s)
{
    // Hash-Array to store the
    // frequency of characters
    int arr[26] = { 0 };
     
    // Loop to find the frequency
    // of the characters
    for (int i = 0; i < s.length(); i++)
        arr[s[i] - 'a']++;
     
    // Hash map to store the occurrence
    // of frequencies of characters
    unordered_map hash;
    for (int i = 0; i < 26; i++)
        if (arr[i] != 0)
            hash[arr[i]]++;
     
    // Loop to find the maximum
    // Repeated frequency from hash-map
    int max_count = 0, res = -1;
    for (auto i : hash) {
        if (max_count < i.second) {
            res = i.first;
            max_count = i.second;
        }
    }
 
    cout <<"Frequency " << res << " is repeated "
         << max_count<<" times";
}
 
// Driver Code
int main()
{
    string s = "geeksgeeks";
     
    // Function Call
    findMaxFrequency(s);
    return 0;
}


Java
// Java implementation to find the
// maximum repeated frequency of
// characters in the given String
import java.util.*;
 
class GFG{
 
// Function to find the maximum
// repeated frequency of the
// characters in the given String
static void findMaxFrequency(String s)
{
    // Hash-Array to store the
    // frequency of characters
    int arr[] = new int[26];
     
    // Loop to find the frequency
    // of the characters
    for (int i = 0; i < s.length(); i++)
        arr[s.charAt(i) - 'a']++;
     
    // Hash map to store the occurrence
    // of frequencies of characters
    HashMap hash = new HashMap();
    for (int i = 0; i < 26; i++)
        if (arr[i] != 0) {
            if(hash.containsKey(arr[i])){
                hash.put(arr[i], hash.get(arr[i])+1);
            }
            else{
                hash.put(arr[i], 1);
            }
        }
     
    // Loop to find the maximum
    // Repeated frequency from hash-map
    int max_count = 0, res = -1;
    for (Map.Entry i : hash.entrySet()){
        if (max_count < i.getValue()) {
            res = i.getKey();
            max_count = i.getValue();
        }
    }
 
    System.out.println("Frequency " + res+ " is repeated "
        + max_count+" times");
}
 
// Driver Code
public static void main(String[] args)
{
    String s = "geeksgeeks";
     
    // Function Call
    findMaxFrequency(s);
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 implementation to find the
# maximum repeated frequency of
# characters in the given string
 
# Function to find the maximum
# repeated frequency of the
# characters in the given string
def findMaxFrequency(s):
     
    # Hash-Array to store the
    # frequency of characters
     
    arr = [0]*26
     
    # Loop to find the frequency
    # of the characters
    for i in range(len(s)):
        arr[ord(s[i]) - ord('a')] += 1
         
    # Hash map to store the occurrence
    # of frequencies of characters
     
    hash = {}
    for i in range(26):
        if (arr[i] != 0):
            if arr[i] not in hash:
                hash[arr[i]] = 0
            hash[arr[i]] += 1
             
    # Loop to find the maximum
    # Repeated frequency from hash-map
    max_count = 0
    res = -1
    for i in hash:
        if (max_count < hash[i]):
            res = i
            max_count = hash[i]
     
    print("Frequency", res, "is repeated", max_count, "times")
 
# Driver Code
 
s = "geeksgeeks"
 
# Function Call
findMaxFrequency(s)
 
# This code is contributed by shubhamsingh10


C#
// C# implementation to find the
// maximum repeated frequency of
// characters in the given String
using System;
using System.Collections.Generic;
 
class GFG{
  
// Function to find the maximum
// repeated frequency of the
// characters in the given String
static void findMaxFrequency(String s)
{
    // Hash-Array to store the
    // frequency of characters
    int []arr = new int[26];
      
    // Loop to find the frequency
    // of the characters
    for (int i = 0; i < s.Length; i++)
        arr[s[i] - 'a']++;
      
    // Hash map to store the occurrence
    // of frequencies of characters
    Dictionary hash = new Dictionary();
    for (int i = 0; i < 26; i++)
        if (arr[i] != 0) {
            if(hash.ContainsKey(arr[i])){
                hash[arr[i]] = hash[arr[i]]+1;
            }
            else{
                hash.Add(arr[i], 1);
            }
        }
      
    // Loop to find the maximum
    // Repeated frequency from hash-map
    int max_count = 0, res = -1;
    foreach( KeyValuePair i in hash){
        if (max_count < i.Value) {
            res = i.Key;
            max_count = i.Value;
        }
    }
  
    Console.WriteLine("Frequency " + res+ " is repeated "
        + max_count+" times");
}
  
// Driver Code
public static void Main(String[] args)
{
    String s = "geeksgeeks";
      
    // Function Call
    findMaxFrequency(s);
}
}
  
 
// This code is contributed by 29AjayKumar


Javascript


输出:
Frequency 2 is repeated 3 times

性能分析:

  • 时间复杂度: O(N)
  • 辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程