Check if the frequency of any character is more than half the length of the string
Given a string str, the task is to check if the frequency of any character is more than half the length of the given string. The characters can be lowercase or uppercase alphabets, digits and special characters.
Examples:
Input: str = “AAa*2AAAA”
Output: Yes
The frequency of ‘A’ is more than half the length of the string.
Input: str = “abB@2a”
Output: No
Approach: The problem can be easily solved by using a frequency array of length 28 i.e. 256 as there are 256 different characters. Iterate through the string and increase the count of the character by one in the frequency array every time it is encountered. Finally, iterate through the frequency array to check if the frequency of any character is more than half the length of the string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAXN 256
bool checkHalfFrequency(string str)
{
int L = str.size();
int fre[MAXN] = { 0 };
for ( int i = 0; i < L; i++)
fre[str[i]]++;
for ( int i = 0; i < MAXN; i++)
if (fre[i] > L / 2)
return true ;
return false ;
}
int main()
{
string str = "GeeksforGeeks" ;
if (checkHalfFrequency(str))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
static int MAXN = 256 ;
static boolean checkHalfFrequency(String str)
{
int L = str.length();
int fre[] = new int [MAXN];
for ( int i = 0 ; i < L; i++)
{
fre[str.charAt(i)]++;
}
for ( int i = 0 ; i < MAXN; i++)
{
if (fre[i] > L / 2 )
{
return true ;
}
}
return false ;
}
public static void main(String[] args)
{
String str = "GeeksforGeeks" ;
if (checkHalfFrequency(str))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
MAXN = 256
def checkHalfFrequency( Str ):
L = len ( Str )
fre = [ 0 for i in range (MAXN)]
for i in range (L):
fre[ ord ( Str [i])] + = 1
for i in range (MAXN):
if (fre[i] > L / / 2 ):
return True
return False
Str = "GeeksforGeeks"
if (checkHalfFrequency( Str )):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static int MAXN = 256;
static bool checkHalfFrequency( string str)
{
int L = str.Length;
int [] fre = new int [MAXN];
for ( int i = 0; i < L; i++)
{
fre[str[i]]++;
}
for ( int i = 0; i < MAXN; i++)
{
if (fre[i] > L / 2)
{
return true ;
}
}
return false ;
}
public static void Main()
{
string str = "GeeksforGeeks" ;
if (checkHalfFrequency(str))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
PHP
<?php
function checkHalfFrequency( $str )
{
$MAXN = 256;
$L = strlen ( $str );
$fre = array_fill (0, $MAXN , 0 );
for ( $i = 0; $i < $L ; $i ++)
{
$fre [ord( $str [ $i ])]++;
}
for ( $i = 0; $i < $MAXN ; $i ++)
{
if ( $fre [ $i ] > (int)( $L / 2))
{
return true;
}
}
return false;
}
$str = "GeeksforGeeks" ;
if (checkHalfFrequency( $str ))
{
echo ( "Yes" );
}
else
{
echo ( "No" );
}
|
Javascript
<script>
var MAXN = 256
function checkHalfFrequency(str)
{
var L = str.length;
var fre = Array(MAXN);
for ( var i = 0; i < L; i++)
fre[str[i]]++;
for ( var i = 0; i < MAXN; i++)
if (fre[i] > L / 2)
return true ;
return false ;
}
var str = "GeeksforGeeks" ;
if (checkHalfFrequency(str))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N), N = length of the string
Auxiliary Space: O(1), as no extra space has been taken.
Last Updated :
07 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...