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.
Input: str = “AAa*2AAAA”
The frequency of ‘A’ is more than half the length of the string.
Input: str = “abB@2a”
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:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- 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
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Frequency of each character in a String using unordered_map in C++
- Longest sub-string having frequency of each character less than equal to k
- 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
- Largest index for each distinct character in given string with frequency K
- Maximum length substring with highest frequency in a string
- Minimize the length of string by removing occurrence of only one character
- Check if a string has all characters with same frequency with one variation allowed
- Check whether frequency of characters in a string makes Fibonacci Sequence
- Check if both halves of the string have at least one different character
- Check whether the Average Character of the String is present or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.