📌  相关文章
📜  检查任何字符的频率是否超过字符串长度的一半

📅  最后修改于: 2021-05-07 08:27:11             🧑  作者: Mango

给定字符串str ,任务是检查任何字符的频率是否超过给定字符串长度的一半。字符可以是小写或大写字母,数字和特殊字符。

例子:

方法:使用长度为2 8的频率数组即256可以轻松解决此问题,因为有256个不同的字符。遍历字符串,每次遇到字符,在频率数组中将其计数加一。最后,遍历频率数组以检查任何字符的频率是否大于字符串长度的一半。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define MAXN 256
  
// Function that returns true if the frequency
// of any character is more than half the
// length of the string
bool checkHalfFrequency(string str)
{
  
    // Length of the string
    int L = str.size();
  
    // Initialize a frequency array
    int fre[MAXN] = { 0 };
  
    // Iterate the string and update the
    // frequency of each of the character
    for (int i = 0; i < L; i++)
        fre[str[i]]++;
  
    // Iterate the frequency array
    for (int i = 0; i < MAXN; i++)
  
        // If condition is satisfied
        if (fre[i] > L / 2)
            return true;
  
    return false;
}
  
// Driver code
int main()
{
    string str = "GeeksforGeeks";
  
    if (checkHalfFrequency(str))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of the approach
  
class GFG 
{
  
    static int MAXN = 256;
  
    // Function that returns true if the frequency
    // of any character is more than half the
    // length of the string
    static boolean checkHalfFrequency(String str)
    {
  
        // Length of the string
        int L = str.length();
  
        // Initialize a frequency array
        int fre[] = new int[MAXN];
  
        // Iterate the string and update the
        // frequency of each of the character
        for (int i = 0; i < L; i++)
        {
            fre[str.charAt(i)]++;
        }
  
        // Iterate the frequency array
        for (int i = 0; i < MAXN; i++) // If condition is satisfied
        {
            if (fre[i] > L / 2) 
            {
                return true;
            }
        }
  
        return false;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        String str = "GeeksforGeeks";
  
        if (checkHalfFrequency(str))
        {
            System.out.println("Yes");
        } 
        else
        {
            System.out.println("No");
        }
    }
}
  
/* This code contributed by PrinciRaj1992 */


Python3
# Python3 implementation of the approach
MAXN = 256
  
# Function that returns true if the frequency
# of any character is more than half the
# length of the String
def checkHalfFrequency(Str):
  
    # Length of the String
    L = len(Str)
  
    # Initialize a frequency array
    fre = [0 for i in range(MAXN)]
  
    # Iterate the and update the
    # frequency of each of the character
    for i in range(L):
        fre[ord(Str[i])] += 1
  
    # Iterate the frequency array
    for i in range(MAXN):
  
        # If condition is satisfied
        if (fre[i] > L // 2):
            return True
  
    return False
  
# Driver code
Str = "GeeksforGeeks"
  
if (checkHalfFrequency(Str)):
    print("Yes")
else:
    print("No")
  
# This code is contributed by mohit kumar


C#
// C# implementation of the approach
using System;
  
class GFG
{
  
    static int MAXN = 256;
  
    // Function that returns true if the frequency
    // of any character is more than half the
    // length of the string
    static bool checkHalfFrequency(string str)
    {
  
        // Length of the string
        int L = str.Length;
  
        // Initialize a frequency array
        int[] fre = new int[MAXN];
  
        // Iterate the string and update the
        // frequency of each of the character
        for (int i = 0; i < L; i++)
        {
            fre[str[i]]++;
        }
  
        // Iterate the frequency array
        for (int i = 0; i < MAXN; i++) // If condition is satisfied
        {
            if (fre[i] > L / 2) 
            {
                return true;
            }
        }
  
        return false;
    }
  
    // Driver code
    public static void Main()
    {
        string str = "GeeksforGeeks";
  
        if (checkHalfFrequency(str))
        {
            Console.WriteLine("Yes");
        } 
        else
        {
            Console.WriteLine("No");
        }
    }
}
  
/* This code contributed by Code_Mech */


PHP
 (int)($L / 2)) 
        {
            return true;
        }
    }
  
    return false;
}
  
// Driver code
$str = "GeeksforGeeks";
  
if (checkHalfFrequency($str))
{
    echo("Yes");
} 
else
{
    echo("No");
}
  
// This code is contributed by Code_Mech


输出:
No