Program to check if all characters have even frequency
Last Updated :
16 Mar, 2023
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++
#include <iostream>
using namespace std;
bool check(string s)
{
int freq[26] = {0};
int n = s.length();
for ( int i = 0; i < s.length(); i++)
freq[s[i] - 97]++;
for ( int i = 0; i < 26; i++)
if (freq[i] % 2 == 1)
return false ;
return true ;
}
int main()
{
string s = "abaccaba" ;
check(s) ? cout << "Yes" << endl :
cout << "No" << endl;
return 0;
}
|
Java
class GFG
{
static boolean check(String s)
{
int [] freq = new int [ 26 ];
int n = s.length();
for ( int i = 0 ; i < s.length(); i++)
{
freq[(s.charAt(i)) - 97 ] += 1 ;
}
for ( int i = 0 ; i < freq.length; i++)
{
if (freq[i] % 2 == 1 )
{
return false ;
}
}
return true ;
}
public static void main(String[] args)
{
String s = "abaccaba" ;
if (check(s))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
def check(s):
freq = [ 0 ] * 26
n = len (s)
for i in range (n):
freq[ ord (s[i]) - 97 ] + = 1
for i in range ( 26 ):
if (freq[i] % 2 = = 1 ):
return False
return True
s = "abaccaba"
if (check(s)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static Boolean check(String s)
{
int [] freq = new int [26];
int n = s.Length;
for ( int i = 0; i < s.Length; i++)
{
freq[(s[i]) - 97] += 1;
}
for ( int i = 0; i < freq.Length; i++)
{
if (freq[i] % 2 == 1)
{
return false ;
}
}
return true ;
}
public static void Main(String[] args)
{
String s = "abaccaba" ;
if (check(s))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
function check(s)
{
var freq = Array(26).fill(0);
var n = s.length;
for ( var i = 0; i < s.length; i++)
freq[s[i] - 97]++;
for ( var i = 0; i < 26; i++)
if (freq[i] % 2 == 1)
return false ;
return true ;
}
var s = "abaccaba" ;
check(s) ? document.write( "Yes" ) :
document.write( "No" );
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(26) ? O(1), no extra space is required, so it is a constant.
Method #2:Using built-in python functions.
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 types of characters
C++
#include <iostream>
#include <unordered_map>
using namespace std;
bool checkString(string s) {
unordered_map< char , int > frequency;
for ( int i = 0; i < s.length(); i++) {
char ch = s.at(i);
frequency[ch]++;
}
for ( auto value : frequency) {
if (value.second % 2 == 1) {
return false ;
}
}
return true ;
}
int main() {
string s = "geeksforgeeksfor" ;
if (checkString(s)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
|
Java
import java.util.Map;
import java.util.HashMap;
public class Main {
static boolean checkString(String s) {
Map<Character, Integer> frequency = new HashMap<>();
for ( int i = 0 ; i < s.length(); i++) {
char ch = s.charAt(i);
frequency.put(ch, frequency.getOrDefault(ch, 0 ) + 1 );
}
for ( int value : frequency.values()) {
if (value % 2 == 1 ) {
return false ;
}
}
return true ;
}
public static void main(String[] args) {
String s = "geeksforgeeksfor" ;
if (checkString(s)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
Python3
from collections import Counter
def checkString(s):
frequency = Counter(s)
for i in frequency:
if (frequency[i] % 2 = = 1 ):
return False
return True
s = "geeksforgeeksfor"
if (checkString(s)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class MainClass {
static bool CheckString( string s) {
Dictionary< char , int > frequency = new Dictionary< char , int >();
foreach ( char ch in s) {
if (frequency.ContainsKey(ch)) {
frequency[ch]++;
} else {
frequency.Add(ch, 1);
}
}
foreach ( int value in frequency.Values) {
if (value % 2 == 1) {
return false ;
}
}
return true ;
}
public static void Main( string [] args) {
string s = "geeksforgeeksfor" ;
if (CheckString(s)) {
Console.WriteLine( "Yes" );
} else {
Console.WriteLine( "No" );
}
}
}
|
Javascript
function checkString(s)
{
let frequency = new Map();
for (let i = 0; i < s.length; i++) {
if (frequency.has(s[i])) {
frequency.set(s[i], frequency.get(s[i]) + 1);
} else {
frequency.set(s[i], 1);
}
}
for (let value of frequency.values())
{
if (value % 2 == 1) {
return false ;
}
}
return true ;
}
let s = "geeksforgeeksfor" ;
if (checkString(s)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...