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++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define MAXN 256 // Function that returns true if the frequency // of any character is more than half the // length of the string bool checkHalfFrequency(string str) { // Length of the string int L = str.size(); // Initialize a frequency array int fre[MAXN] = { 0 }; // Iterate the string and update the // frequency of each of the character for (int i = 0; i < L; i++) fre[str[i]]++; // Iterate the frequency array for (int i = 0; i < MAXN; i++) // If condition is satisfied if (fre[i] > L / 2) return true; return false; } // Driver code int main() { string str = "GeeksforGeeks"; if (checkHalfFrequency(str)) cout << "Yes"; else cout << "No"; return 0; } |
Java
// Java implementation of the approach class GFG { static int MAXN = 256; // Function that returns true if the frequency // of any character is more than half the // length of the string static boolean checkHalfFrequency(String str) { // Length of the string int L = str.length(); // Initialize a frequency array int fre[] = new int[MAXN]; // Iterate the string and update the // frequency of each of the character for (int i = 0; i < L; i++) { fre[str.charAt(i)]++; } // Iterate the frequency array for (int i = 0; i < MAXN; i++) // If condition is satisfied { if (fre[i] > L / 2) { return true; } } return false; } // Driver code public static void main(String[] args) { String str = "GeeksforGeeks"; if (checkHalfFrequency(str)) { System.out.println("Yes"); } else { System.out.println("No"); } } } /* This code contributed by PrinciRaj1992 */ |
Python3
# Python3 implementation of the approach MAXN = 256 # Function that returns true if the frequency # of any character is more than half the # length of the String def checkHalfFrequency(Str): # Length of the String L = len(Str) # Initialize a frequency array fre = [0 for i in range(MAXN)] # Iterate the and update the # frequency of each of the character for i in range(L): fre[ord(Str[i])] += 1 # Iterate the frequency array for i in range(MAXN): # If condition is satisfied if (fre[i] > L // 2): return True return False # Driver code Str = "GeeksforGeeks" if (checkHalfFrequency(Str)): print("Yes") else: print("No") # This code is contributed by mohit kumar |
C#
// C# implementation of the approach using System; class GFG { static int MAXN = 256; // Function that returns true if the frequency // of any character is more than half the // length of the string static bool checkHalfFrequency(string str) { // Length of the string int L = str.Length; // Initialize a frequency array int[] fre = new int[MAXN]; // Iterate the string and update the // frequency of each of the character for (int i = 0; i < L; i++) { fre[str[i]]++; } // Iterate the frequency array for (int i = 0; i < MAXN; i++) // If condition is satisfied { if (fre[i] > L / 2) { return true; } } return false; } // Driver code public static void Main() { string str = "GeeksforGeeks"; if (checkHalfFrequency(str)) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } } } /* This code contributed by Code_Mech */ |
PHP
<?php // PHP implementation of the approach // Function that returns true if the frequency // of any character is more than half the // length of the string function checkHalfFrequency($str) { $MAXN = 256; // Length of the string $L = strlen($str); // Initialize a frequency array $fre = array_fill(0, $MAXN, 0 ); // Iterate the string and update the // frequency of each of the character for ($i = 0; $i < $L; $i++) { $fre[ord($str[$i])]++; } // Iterate the frequency array for ($i = 0; $i < $MAXN; $i++) // If condition is satisfied { if ($fre[$i] > (int)($L / 2)) { return true; } } return false; } // Driver code $str = "GeeksforGeeks"; if (checkHalfFrequency($str)) { echo("Yes"); } else{ echo("No"); } // This code is contributed by Code_Mech |
No
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count number of rotated strings which have more number of vowels in the first half than second half
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Replace every character of string by character whose ASCII value is K times more than it
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Check if a String contains any index with more than K active characters
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Find the sum of the first half and second half elements of an array
- Find prime numbers in the first half and second half of an array
- Find the XOR of first half and second half elements of an array
- Reorder an array such that sum of left half is not equal to sum of right half
- Longest sub-string having frequency of each character less than equal to k
- Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's
- Find a string such that every character is lexicographically greater than its immediate next character
- Frequency of smallest character in first sentence less than that of second sentence
- Check if frequency of each character is equal to its position in English Alphabet
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Index of character depending on frequency count in string
- Frequency of each character in a String using unordered_map in C++
- Largest index for each distinct character in given string with frequency K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

