📌  相关文章
📜  检查所有字符是否具有均匀频率的程序

📅  最后修改于: 2022-05-13 01:57:07.760000             🧑  作者: Mango

检查所有字符是否具有均匀频率的程序

给定一个仅由小写字母组成的字符串S,检查该字符串是否所有字符都出现偶数次。
例子:

方法:
我们将遍历字符串并计算所有字符的出现次数,然后我们将检查出现是否偶数,如果有任何奇数频率字符,则立即打印 No。

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
bool check(string s)
{
     
    // creating a frequency array
    int freq[26] = {0};
     
    // Finding length of s
    int n = s.length();
    for (int i = 0; i < s.length(); i++)
     
    // counting frequency of all characters
        freq[s[i] - 97]++;
     
    // checking if any odd frequency
    // is there or not
    for (int i = 0; i < 26; i++)
        if (freq[i] % 2 == 1)
        return false;
    return true;
}
 
// Driver Code
int main()
{
    string s = "abaccaba";
    check(s) ? cout << "Yes" << endl :
               cout << "No" << endl;
    return 0;
}
 
// This code is contributed by
// sanjeev2552


Java
// Java implementation of the above approach
class GFG
{
    static boolean check(String s)
    {
 
        // creating a frequency array
        int[] freq = new int[26];
 
        // Finding length of s
        int n = s.length();
 
        // counting frequency of all characters
        for (int i = 0; i < s.length(); i++)
        {
            freq[(s.charAt(i)) - 97] += 1;
        }
 
        // checking if any odd frequency
        // is there or not
        for (int i = 0; i < freq.length; i++)
        {
            if (freq[i] % 2 == 1)
            {
                return false;
            }
        }
        return true;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String s = "abaccaba";
        if (check(s))
        {
            System.out.println("Yes");
        }
        else
        {
            System.out.println("No");
        }
    }
}
 
// This code is contributed by Rajput-Ji


Python3
# Python implementation of the above approach
def check(s):
 
    # creating a frequency array
    freq =[0]*26
 
    # Finding length of s
    n = len(s)
 
    for i in range(n):
 
        # counting frequency of all characters
        freq[ord(s[i])-97]+= 1
 
    for i in range(26):
 
        # checking if any odd frequency
        # is there or not
        if (freq[i]% 2 == 1):
            return False
    return True
 
# Driver code
s ="abaccaba"
if(check(s)):
    print("Yes")
else:
    print("No")


C#
// C# implementation of the approach
using System;
     
class GFG
{
    static Boolean check(String s)
    {
 
        // creating a frequency array
        int[] freq = new int[26];
 
        // Finding length of s
        int n = s.Length;
 
        // counting frequency of all characters
        for (int i = 0; i < s.Length; i++)
        {
            freq[(s[i]) - 97] += 1;
        }
 
        // checking if any odd frequency
        // is there or not
        for (int i = 0; i < freq.Length; i++)
        {
            if (freq[i] % 2 == 1)
            {
                return false;
            }
        }
        return true;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        String s = "abaccaba";
        if (check(s))
        {
            Console.WriteLine("Yes");
        }
        else
        {
            Console.WriteLine("No");
        }
    }
}
 
// This code is contributed by PrinciRaj1992


Javascript


Python3
# Python implementation for
# the above approach
 
# importing Counter function
from collections import Counter
 
# Function to check if all
# elements occur even times
def checkString(s):
   
    # Counting the frequency of all
    # character using Counter function
    frequency = Counter(s)
     
    # Traversing frequency
    for i in frequency:
       
        # Checking if any element
        # has odd count
        if (frequency[i] % 2 == 1):
            return False
    return True
 
 
# Driver code
s = "geeksforgeeksfor"
if(checkString(s)):
    print("Yes")
else:
    print("No")
     
# This code is contributed by vikkycirus


输出:

Yes

方法#2:使用内置的Python函数。

方法

我们将扫描字符串并使用内置的Counter()函数计算所有字符的出现次数,然后遍历计数器列表并检查出现是否偶数,如果有任何奇数频率字符,则立即打印 No。

注意:此方法适用于所有类型的字符

Python3

# Python implementation for
# the above approach
 
# importing Counter function
from collections import Counter
 
# Function to check if all
# elements occur even times
def checkString(s):
   
    # Counting the frequency of all
    # character using Counter function
    frequency = Counter(s)
     
    # Traversing frequency
    for i in frequency:
       
        # Checking if any element
        # has odd count
        if (frequency[i] % 2 == 1):
            return False
    return True
 
 
# Driver code
s = "geeksforgeeksfor"
if(checkString(s)):
    print("Yes")
else:
    print("No")
     
# This code is contributed by vikkycirus

输出

Yes