Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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(27)

Optimized Code:

One optimization that can be made is to reduce the size of the hash array. Since the code only needs to count the number of odd and even characters in the string, the size of the hash array can be reduced to 2, one for odd characters and one for even characters.

Here’s the optimized code:

C++




#include <bits/stdc++.h>
using namespace std;
 
string oddEven(string S)
{
    int odd = 0, even = 0;
     
    for (int i = 0; i < S.size(); i++) {
        if ((S[i] - 'a') % 2 == 0)
            even++;
        else
            odd++;
    }
     
    if (odd % 2 == 1)
        return "ODD";
    else
        return "EVEN";
}
 
int main()
{
    string S = "abbbcc";
    cout << oddEven(S);
    return 0;
}


Java




import java.util.*;
 
public class Main {
    public static String oddEven(String S) {
        int odd = 0, even = 0;
         
        for (int i = 0; i < S.length(); i++) {
            if ((S.charAt(i) - 'a') % 2 == 0)
                even++;
            else
                odd++;
        }
         
        if (odd % 2 == 1)
            return "ODD";
        else
            return "EVEN";
    }
     
    public static void main(String[] args) {
        String S = "abbbcc";
        System.out.println(oddEven(S));
    }
}


C#




using System;
 
namespace OddEven
{
    class Program
    {
        static string OddEven(string S)
        {
            int odd = 0, even = 0;
 
            for (int i = 0; i < S.Length; i++)
            {
                if ((S[i] - 'a') % 2 == 0)
                    even++;
                else
                    odd++;
            }
 
            if (odd % 2 == 1)
                return "ODD";
            else
                return "EVEN";
        }
 
        static void Main(string[] args)
        {
            string S = "abbbcc";
            Console.WriteLine(OddEven(S));
        }
    }
}


Javascript




function oddEven(S) {
  let odd = 0;
  let even = 0;
 
  for (let i = 0; i < S.length; i++) {
    if ((S.charCodeAt(i) - 97) % 2 === 0) {
      even++;
    } else {
      odd++;
    }
  }
 
  if (odd % 2 === 1) {
    return "ODD";
  } else {
    return "EVEN";
  }
}
 
const S = "abbbcc";
console.log(oddEven(S));


Python3




def oddEven(S):
    odd = 0
    even = 0
    for i in range(len(S)):
        if (ord(S[i]) - ord('a')) % 2 == 0:
            even += 1
        else:
            odd += 1
     
    if odd % 2 == 1:
        return "ODD"
    else:
        return "EVEN"
 
S = "abbbcc"
print(oddEven(S))


Output

ODD

This optimization reduces the time complexity to O(n), which is the same as the original code, but it reduces the space complexity from O(27) to O(1).

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



Last Updated : 14 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads