Given a stringĀ sĀ consisting of lowercase English letters, find if sum of two values x and y is even.
- x is the number of distinct characters thatĀ are at even positions in the English alphabet (b, d, f, ...), andĀ appear even times in the string.
- y is the number of distinct characters thatĀ are at odd positions in the English alphabet (a, c, e, ...), andĀ appear oddĀ times in the string.
Return true if x + y is even; otherwise, return false.
Examples:
Input: s = "abbbcc"
Output: false
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 and 'c' occupies 3rd place(odd) but its frequency is even(2) so it also doesn't get counted. x = 0 and y = 1 so (x + y) is ODD, Hence we return false.Input: s = "nobitaa"
Output: true
Explanation: Here n, b, t & a would not count since it doesn't match with the even condition but o & i will be counted as it satisfies the odd conditions so x = 0 and y = 2 so (x + y) is EVEN, Hence we return true.
Table of Content
[Naive Approach] Count Frequency for Each Character - O(26 * n) Time and O(1) Space
The idea is to check each character separately and count its frequency by traversing the entire string. Then, count the characters that satisfy the given parity conditions.
Working of Approach:
- Traverse all characters from 'a' to 'z'.
- For each character, traverse the entire string to count its frequency.
- Check if its alphabet position and frequency satisfy the required condition.
- Increment cnt for every valid distinct character.
- Return true if cnt is even; otherwise, return false.
#include <iostream>
#include <string>
using namespace std;
bool isEven(string &s)
{
int cnt = 0;
// Check every lowercase English character.
for (char ch = 'a'; ch <= 'z'; ch++)
{
int freq = 0;
// Count the frequency of the current character.
for (char c : s)
{
if (c == ch)
freq++;
}
// Skip characters that are not present in the string.
if (freq == 0)
continue;
int pos = ch - 'a' + 1;
// Check the required position and frequency parity conditions.
if ((pos % 2 == 0 && freq % 2 == 0) || (pos % 2 == 1 && freq % 2 == 1))
cnt++;
}
// Return true if the number of valid characters is even.
return cnt % 2 == 0;
}
int main()
{
string s = "nobitaa";
cout << (isEven(s) ? "true" : "false") << endl;
return 0;
}
import java.util.*;
class GFG {
public boolean isEven(String s)
{
int cnt = 0;
// Check every lowercase English character.
for (char ch = 'a'; ch <= 'z'; ch++) {
int freq = 0;
// Count the frequency of the current character.
for (char c : s.toCharArray()) {
if (c == ch)
freq++;
}
// Skip characters that are not present in the
// string.
if (freq == 0)
continue;
int pos = ch - 'a' + 1;
// Check the required position and frequency
// parity conditions.
if ((pos % 2 == 0 && freq % 2 == 0)
|| (pos % 2 == 1 && freq % 2 == 1)) {
cnt++;
}
}
return cnt % 2 == 0;
}
public static void main(String[] args)
{
String s = "nobitaa";
GFG obj = new GFG();
System.out.println(obj.isEven(s) ? "true"
: "false");
}
}
def isEven(s):
cnt = 0
# Check every lowercase English character.
for ch in range(ord('a'), ord('z') + 1):
freq = s.count(chr(ch))
# Skip characters that are not present in the string.
if freq == 0:
continue
pos = ch - ord('a') + 1
# Check the required position and frequency parity conditions.
if (pos % 2 == 0 and freq % 2 == 0) or (pos % 2 == 1 and freq % 2 == 1):
cnt += 1
# Return true if the number of valid characters is even.
return cnt % 2 == 0
if __name__ == "__main__":
s = "nobitaa"
print("true" if isEven(s) else "false")
using System;
class GFG {
static bool isEven(string s)
{
int cnt = 0;
// Check every lowercase English character.
for (char ch = 'a'; ch <= 'z'; ch++) {
int freq = 0;
// Count the frequency of the current character.
foreach(char c in s)
{
if (c == ch)
freq++;
}
// Skip characters that are not present in the
// string.
if (freq == 0)
continue;
int pos = ch - 'a' + 1;
// Check the required position and frequency
// parity conditions.
if ((pos % 2 == 0 && freq % 2 == 0)
|| (pos % 2 == 1 && freq % 2 == 1))
cnt++;
}
// Return true if the number of valid characters is
// even.
return cnt % 2 == 0;
}
static void Main()
{
string s = "nobitaa";
Console.WriteLine(isEven(s) ? "true" : "false");
}
}
function isEven(s)
{
let cnt = 0;
// Check every lowercase English character.
for (let ch = "a".charCodeAt(0);
ch <= "z".charCodeAt(0); ch++) {
let freq = 0;
// Count the frequency of the current character.
for (let c of s) {
if (c === String.fromCharCode(ch))
freq++;
}
// Skip characters that are not present in the
// string.
if (freq === 0)
continue;
let pos = ch - "a".charCodeAt(0) + 1;
// Check the required position and frequency parity
// conditions.
if ((pos % 2 === 0 && freq % 2 === 0)
|| (pos % 2 === 1 && freq % 2 === 1))
cnt++;
}
// Return true if the number of valid characters is
// even.
return cnt % 2 === 0;
}
// Driver Code
let s = "nobitaa";
console.log(isEven(s) ? "true" : "false");
Output
true
[Expected Approach] Using Frequency Counting - O(n) Time and O(1) Space
The idea is to first count the frequency of all characters using a frequency array. Then, check which distinct characters satisfy the required parity conditions.
Working of Approach:
- Create a frequency array of size 26.
- Traverse the string and count the frequency of every character.
- Traverse the frequency array and skip characters with zero frequency.
- Check whether the character satisfies either of the required parity conditions.
- Count valid characters and return whether the count is even.
Let us understand with an example:
Input: s = "nobitaa"
- For s = "nobitaa", the frequency array counts each character, where 'a' appears 2 times and the other characters appear once.
- 'i' and 'o' are at odd alphabet positions and both appear an odd number of times, so cnt becomes 2.
- n, b, t, and a do not satisfy their respective alphabet-position and frequency parity conditions.
- Thus, cnt = 2, and 2 % 2 == 0.
- Therefore, the function returns true.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool isEven(string &s)
{
vector<int> freq(26, 0);
// Count the frequency of each character.
for (char ch : s)
freq[ch - 'a']++;
int cnt = 0;
// Count valid distinct characters satisfying the required parity conditions.
for (int i = 0; i < 26; i++)
{
if (freq[i] == 0)
continue;
if (((i + 1) % 2 == 0 && freq[i] % 2 == 0) || ((i + 1) % 2 == 1 && freq[i] % 2 == 1))
cnt++;
}
return (cnt % 2 == 0);
}
int main()
{
string s = "nobitaa";
cout << (isEven(s) ? "true" : "false") << endl;
return 0;
}
import java.util.*;
class GFG {
public boolean isEven(String s)
{
int[] freq = new int[26];
// Count the frequency of each character.
for (char ch : s.toCharArray())
freq[ch - 'a']++;
int cnt = 0;
// Count valid distinct characters satisfying the
// required parity conditions.
for (int i = 0; i < 26; i++) {
if (freq[i] == 0)
continue;
if (((i + 1) % 2 == 0 && freq[i] % 2 == 0)
|| ((i + 1) % 2 == 1 && freq[i] % 2 == 1))
cnt++;
}
return cnt % 2 == 0;
}
public static void main(String[] args)
{
String s = "nobitaa";
GFG obj = new GFG();
System.out.println(obj.isEven(s) ? "true"
: "false");
}
}
def isEven(s):
freq = [0] * 26
# Count the frequency of each character.
for ch in s:
freq[ord(ch) - ord('a')] += 1
cnt = 0
# Count valid distinct characters satisfying the required parity conditions.
for i in range(26):
if freq[i] == 0:
continue
if ((i + 1) % 2 == 0 and freq[i] % 2 == 0) or ((i + 1) % 2 == 1 and freq[i] % 2 == 1):
cnt += 1
return cnt % 2 == 0
if __name__ == "__main__":
s = "nobitaa"
print("true" if isEven(s) else "false")
using System;
class GFG {
static bool isEven(string s)
{
int[] freq = new int[26];
// Count the frequency of each character.
foreach(char ch in s) freq[ch - 'a']++;
int cnt = 0;
// Count valid distinct characters satisfying the
// required parity conditions.
for (int i = 0; i < 26; i++) {
if (freq[i] == 0)
continue;
if (((i + 1) % 2 == 0 && freq[i] % 2 == 0)
|| ((i + 1) % 2 == 1 && freq[i] % 2 == 1))
cnt++;
}
return cnt % 2 == 0;
}
static void Main()
{
string s = "nobitaa";
Console.WriteLine(isEven(s) ? "true" : "false");
}
}
function isEven(s)
{
let freq = new Array(26).fill(0);
// Count the frequency of each character.
for (let ch of s) {
freq[ch.charCodeAt(0) - "a".charCodeAt(0)]++;
}
let cnt = 0;
// Count valid distinct characters satisfying the
// required parity conditions.
for (let i = 0; i < 26; i++) {
if (freq[i] === 0)
continue;
if (((i + 1) % 2 === 0 && freq[i] % 2 === 0)
|| ((i + 1) % 2 === 1 && freq[i] % 2 === 1))
cnt++;
}
return cnt % 2 === 0;
}
// Driver Code
let s = "nobitaa";
console.log(isEven(s) ? "true" : "false");
Output
true