Parity of count of letters whose position and frequency have same parity
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++
#include <bits/stdc++.h>
using namespace std;
string oddEven(string S)
{
int hash[27] = { 0 };
int x = 0, y = 0;
for ( int i = 0; i < S.size(); i++) {
hash[S[i] - 'a' + 1]++;
}
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" ;
}
int main()
{
string S = "abbbcc" ;
cout << oddEven(S);
return 0;
}
|
Java
public class GFG {
static String oddEven(String S)
{
int hash[] = new int [ 27 ];
int x = 0 , y = 0 ;
for ( int i = 0 ; i < S.length(); i++) {
hash[S.charAt(i) - 'a' + 1 ]++;
}
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" ;
}
public static void main (String[] args)
{
String S = "abbbcc" ;
System.out.println(oddEven(S));
}
}
|
Python3
def oddEven(S):
s = list (S)
hash = [ 0 ] * 27
x = 0 ; y = 0 ;
for i in range ( len (S)) :
hash [ ord (s[i]) - 96 ] + = 1
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"
if __name__ = = "__main__" :
S = "abbbcc"
print (oddEven(S))
|
C#
using System;
public class GCD {
static string oddEven( string S)
{
int [] hash = new int [27];
int x = 0, y = 0;
for ( int i = 0; i < S.Length; i++)
{
hash[S[i] - 'a' + 1]++;
}
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" ;
}
public static void Main()
{
string S = "abbbcc" ;
Console.Write(oddEven(S));
}
}
|
Javascript
<script>
function oddEven(S)
{
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;
}
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" ;
}
}
var S = "abbbcc" ;
document.write(oddEven(S));
</script>
|
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))
|
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
Share your thoughts in the comments
Please Login to comment...