Skip to content
Related Articles

Related Articles

Parity of count of letters whose position and frequency have same parity

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 11 Jul, 2022
Improve Article
Save Article

Given a string S of lowercase English characters, find out whether the number of the letters whose frequencies in the string have the same parity as their positions in the English alphabet is odd or even (i.e., they have an odd frequency in the string and their position is at an odd number in the English alphabet or have even frequency and their position is at an even number in the English alphabet).

Examples:

Input: S = “abbbcc”
Output: ODD
Explanation: ‘a’ occupies 1st place(odd) in English alphabets and its frequency is odd(1),  
‘b’ occupies 2nd place(even) but its frequency is odd(3) so it doesn’t get counted 
‘c’ occupies 3rd place(odd) but its frequency is even(2) so it also doesn’t get counted.

Input: S = “nobitaa”
Output: EVEN

 

Approach: The problem can be solved following the below idea: 

Store frequency of each character in a new array. Then traverse on that array. While traversing if position of element is even and its frequency is also even then increment or if position of element is odd and its frequency is also odd then increment the count and find the parity of the final count.

Follow the steps mentioned here to implement the idea:

  • Create a hash array of size 27 (to store the frequencies of all the lowercase characters).
  • Create variables x = 0 and y = 0.
  • Traverse on a string and store frequency of each character( S[i] – ‘a’ + 1) in hash array.
  • Traverse on hash array:
    • If the frequency of the character is greater than 0 then check if the index is even and its frequency is also even then increment x,
    • If the index is odd and its frequency is also odd then increment y.
  • if x + y is even then return even. Otherwise, return odd.

Below is the implementation of the above approach.

C++




// C++ code to implement the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the parity
// of the required characters
string oddEven(string S)
{
    // Array to store frequency of each character
    int hash[27] = { 0 };
    int x = 0, y = 0;
 
    for (int i = 0; i < S.size(); i++) {
        hash[S[i] - 'a' + 1]++;
    }
 
    // Traversing on hash array
    for (int i = 1; i < 26; i++) {
        if (hash[i] != 0) {
            if (i % 2 == 0 && hash[i] % 2 == 0)
                x++;
            else if (i % 2 == 1 && hash[i] % 2 == 1)
                y++;
        }
    }
 
    if ((x + y) % 2 == 1)
        return "ODD";
    else
        return "EVEN";
}
 
// Driver code
int main()
{
    string S = "abbbcc";
 
    // Function call
    cout << oddEven(S);
    return 0;
}


Java




// Java program for above approach
  
public class GFG {
  
// Function to find the parity
// of the required characters
static String oddEven(String S)
{
    // Array to store frequency of each character
    int hash[] = new int[27];
 
    int x = 0, y = 0;
 
    for (int i = 0; i < S.length(); i++) {
        hash[S.charAt(i) - 'a' + 1]++;
    }
 
    // Traversing on hash array
    for (int i = 1; i < 26; i++) {
        if (hash[i] != 0) {
            if (i % 2 == 0 && hash[i] % 2 == 0)
                x++;
            else if (i % 2 == 1 && hash[i] % 2 == 1)
                y++;
        }
    }
 
    if ((x + y) % 2 == 1)
        return "ODD";
    else
        return "EVEN";
}
  
    // Driver Code
    public static void main (String[] args)
    {
        String S = "abbbcc";
         
        // Function call
        System.out.println(oddEven(S));
      
    }
}
// This code is contributed by aditya942003patil


Python3




# python3 code to implement the above approach
 
# Function to find the parity
# of the required characters
def oddEven(S):
     
    s = list(S)
    # Array to store frequency of each character
    hash = [0]*27
    x = 0; y = 0;
 
    for i in range(len(S)) :
        hash[ord(s[i]) - 96]+=1
 
    # Traversing on hash array
    for i in range(1,27) :
        if hash[i] != 0 :
            if i % 2 == 0 and hash[i] % 2 == 0 :
                x+=1
            elif i % 2 == 1 and hash[i] % 2 == 1 :
                y+=1
 
    if (x + y) % 2 == 1 :
        return "ODD"
    else :
        return "EVEN"
     
      
# Driver Code
if __name__ == "__main__" :
     
    S = "abbbcc"
 
    # Function call
    print(oddEven(S))
     
# this code is contributed by aditya942003patil


C#




// c# code to implement the above approach
using System;
  
public class GCD {
      
    // Function to find the parity
    // of the required characters
    static string oddEven(string S)
    {
        // Array to store frequency of each character
        int[] hash = new int[27];
        int x = 0, y = 0;
         
        for (int i = 0; i < S.Length; i++)
        {
            hash[S[i] - 'a' + 1]++;
        }
         
        // Traversing on hash array
        for (int i = 1; i < 26; i++)
        {
            if (hash[i] != 0)
            {
                if (i % 2 == 0 && hash[i] % 2 == 0)
                x++;
                else if (i % 2 == 1 && hash[i] % 2 == 1)
                y++;
            }
        }
         
        if ((x + y) % 2 == 1)
        return "ODD";
        else
        return "EVEN";
    }
      
    // Driver Code
    public static void Main()
    {
        string S = "abbbcc";
         
        // Function call
        Console.Write(oddEven(S));
    }
}
  
// This code is contributed by aditya942003patil


Javascript




<script>
// Javascript code to implement the approach.
  
// Function to find the parity
// of the required characters
function oddEven(S)
{
    // Array to store frequency of each character
    var hash = new Array(27);
     
    for (var i = 0; i < 27; i++) {
        hash[i]=0;
    }
     
    var x = 0, y = 0;
     
    for (var i = 0; i < S.length; i++) {
        var p = S.charCodeAt(i);
        hash[p - 96]+=1;
    }
     
    // Traversing on hash array
    for (let i = 1; i < 26; i++) {
        if (hash[i] != 0)
        {
            if (i % 2 == 0 && hash[i] % 2 == 0)
            {
                x++;
            }
            else if (i % 2 == 1 && hash[i] % 2 == 1)
            {
                y++;
            }
        }
    }
     
    if ((x + y) % 2 == 1)
    {
        return "ODD";
    }
    else
    {
        return "EVEN";
    }
     
}
  
    // Driver code
  
    var S = "abbbcc";
 
    // Function call
    document.write(oddEven(S));
      
    // This code is contributed by aditya942003patil.
   </script>


Output

ODD

Time Complexity: O(|S|) where |S| is the size of string S.
Space Complexity: O(1)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!