Program to check if all characters have even frequency - GeeksforGeeks

24 May, 2021
Given a string S consisting only of lowercase letters check if the string has all characters appearing even times. Examples:
Input : abaccaba Output : Yes Explanation: ‘a’ occurs four times, ‘b’ occurs twice, ‘c’ occurs twice and the other letters occur zero times.Input : hthth Output : No
Approach: We will go through the string and count the occurrence of all characters after that we will check if the occurrences are even or not if there is any odd frequency character then we immediately print No.  
C++
Java
Python3
C#
Javascript
// C++ implementation of the above approach#include <iostream>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 Codeint main(){    string s = "abaccaba";    check(s) ? cout << "Yes" << endl :               cout << "No" << endl;    return 0;} // This code is contributed by// sanjeev2552
// Java implementation of the above approachclass 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
# Python implementation of the above approachdef 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 codes ="abaccaba"if(check(s)):    print("Yes")else:    print("No")
// C# implementation of the approachusing 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
<script>  // JavaScript implementation of the above approach function check(s){         // creating a frequency array    var freq = Array(26).fill(0);         // Finding length of s    var n = s.length;    for (var 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 (var i = 0; i < 26; i++)        if (freq[i] % 2 == 1)        return false;    return true;} // Driver Codevar s = "abaccaba";check(s) ? document.write("Yes") :           document.write("No"); </script>
Output: 
Yes
Approach:
We will scan the string and count the occurrence of all characters using the built-in Counter() function after that we traverse the counter list and check if the occurrences are even or not if there is any odd frequency character then we immediately print No. 
Note: This method is applicable for all type of characters
Python3
# Python implementation for# the above approach # importing Counter functionfrom collections import Counter # Function to check if all# elements occur even timesdef 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 codes = "geeksforgeeksfor"if(checkString(s)):    print("Yes")else:    print("No")     # This code is contributed by vikkycirus
Output:
Yes
Rajput-Ji
princiraj1992
sanjeev2552
vikkycirus
famously
frequency-counting
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Top 50 String Coding Problems for Interviews
Print all the duplicates in the input string
Vigenère Cipher
String class in Java | Set 1
sprintf() in C
Print all subsequences of a string
Convert character array to string in C++
Program to count occurrence of a given character in a string
How to Append a Character to a String in C
Naive algorithm for Pattern Searching