Character whose frequency is equal to the sum of frequencies of other characters of the given string
Given a string str consisting of lowercase English alphabets. The task is to find whether there is any character in the string whose frequency is equal to the sum of the frequencies of other characters of the string. If such a character exists then print Yes else print No.
Examples:
Input: str = “hkklkwwwww”
Output: Yes
frequency(w) = frequency(h) + frequency(k) + frequency(l)
4 = 1 + 2 + 1
4 = 4
Input: str = “geeksforgeeks”
Output: No
Approach: If the length of the string is odd then the result will always be No. In case of even length string, calculate the frequency of each of the character of the string and for any character if it’s frequency = half of the length of the string then the result will be Yes else No.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isFrequencyEqual(string str, int len)
{
if (len % 2 == 1)
return false ;
int i, freq[26] = { 0 };
for (i = 0; i < len; i++)
freq[str[i] - 'a' ]++;
for (i = 0; i < 26; i++)
if (freq[i] == len / 2)
return true ;
return false ;
}
int main()
{
string str = "geeksforgeeks" ;
int len = str.length();
if (isFrequencyEqual(str, len))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
static boolean isFrequencyEqual(String str, int len)
{
if (len % 2 == 1 )
{
return false ;
}
int i, freq[] = new int [ 26 ];
for (i = 0 ; i < len; i++)
{
freq[str.charAt(i) - 'a' ]++;
}
for (i = 0 ; i < 26 ; i++)
{
if (freq[i] == len / 2 )
{
return true ;
}
}
return false ;
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
int len = str.length();
if (isFrequencyEqual(str, len))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
def isFrequencyEqual(string, length):
if length % 2 = = 1 :
return False
freq = [ 0 ] * 26
for i in range ( 0 , length):
freq[ ord (string[i]) - ord ( 'a' )] + = 1
for i in range ( 0 , 26 ):
if freq[i] = = length / / 2 :
return True
return False
if __name__ = = "__main__" :
string = "geeksforgeeks"
length = len (string)
if isFrequencyEqual(string, length):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool isFrequencyEqual(String str, int len)
{
if (len % 2 == 1)
{
return false ;
}
int i;
int []freq = new int [26];
for (i = 0; i < len; i++)
{
freq[str[i] - 'a' ]++;
}
for (i = 0; i < 26; i++)
{
if (freq[i] == len / 2)
{
return true ;
}
}
return false ;
}
public static void Main()
{
String str = "geeksforgeeks" ;
int len = str.Length;
if (isFrequencyEqual(str, len))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
PHP
<?php
function isFrequencyEqual( $str , $len )
{
if ( $len % 2 == 1)
return false;
$freq = array ();
for ( $i = 0; $i < 26 ; $i ++)
$freq [ $i ] = 0;
for ( $i = 0; $i < $len ; $i ++)
$freq [ord( $str [ $i ]) - 97]++;
for ( $i = 0; $i < 26 ; $i ++)
if ( $freq [ $i ] == $len / 2)
return true;
return false;
}
$str = "geeksforgeeks" ;
$len = strlen ( $str );
if (isFrequencyEqual( $str , $len ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function isFrequencyEqual(str, len)
{
if (len % 2 == 1)
return false ;
var i, freq=Array(26).fill(0);
for (i = 0; i < len; i++)
freq[str[i] - 'a' ]++;
for (i = 0; i < 26; i++)
if (freq[i] == parseInt(len / 2))
return true ;
return false ;
}
var str = "geeksforgeeks" ;
var len = str.length;
if (isFrequencyEqual(str, len))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(len) where len is the length of the given string.
Auxiliary Space: O(26) ? O(1), no extra space is required, so it is a constant.
Last Updated :
19 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...