# 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 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function that returns true if some character ` `// exists in the given string whose frequency ` `// is equal to the sum frequencies of ` `// other characters of the string ` `bool` `isFrequencyEqual(string str, ``int` `len) ` `{ ` ` `  `    ``// If string is of odd length ` `    ``if` `(len % 2 == 1) ` `        ``return` `false``; ` ` `  `    ``// To store the frequency of each ` `    ``// character of the string ` `    ``int` `i, freq[26] = { 0 }; ` ` `  `    ``// Update the frequencies of the characters ` `    ``for` `(i = 0; i < len; i++) ` `        ``freq[str[i] - ``'a'``]++; ` ` `  `    ``for` `(i = 0; i < 26; i++) ` `        ``if` `(freq[i] == len / 2) ` `            ``return` `true``; ` ` `  `    ``// No such character exists ` `    ``return` `false``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string str = ``"geeksforgeeks"``; ` `    ``int` `len = str.length(); ` `    ``if` `(isFrequencyEqual(str, len)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` ` `  `    ``return` `0; ` `} `

 `// Java implementation of the above approach. ` `class` `GFG  ` `{ ` ` `  `    ``// Function that returns true if some character  ` `    ``// exists in the given string whose frequency  ` `    ``// is equal to the sum frequencies of  ` `    ``// other characters of the string  ` `    ``static` `boolean` `isFrequencyEqual(String str, ``int` `len) ` `    ``{ ` ` `  `        ``// If string is of odd length  ` `        ``if` `(len % ``2` `== ``1``)  ` `        ``{ ` `            ``return` `false``; ` `        ``} ` ` `  `        ``// To store the frequency of each  ` `        ``// character of the string  ` `        ``int` `i, freq[] = ``new` `int``[``26``]; ` ` `  `        ``// Update the frequencies of the characters  ` `        ``for` `(i = ``0``; i < len; i++)  ` `        ``{ ` `            ``freq[str.charAt(i) - ``'a'``]++; ` `        ``} ` ` `  `        ``for` `(i = ``0``; i < ``26``; i++) ` `        ``{ ` `            ``if` `(freq[i] == len / ``2``)  ` `            ``{ ` `                ``return` `true``; ` `            ``} ` `        ``} ` ` `  `        ``// No such character exists  ` `        ``return` `false``; ` `    ``} ` ` `  `    ``// Driver code  ` `    ``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"``); ` `        ``} ` `    ``} ` `} ` ` `  `// This code contributed by Rajput-Ji `

 `# Python3 implementation of the approach  ` ` `  `# Function that returns true if some character  ` `# exists in the given string whose frequency  ` `# is equal to the sum frequencies of  ` `# other characters of the string  ` `def` `isFrequencyEqual(string, length):  ` ` `  `    ``# If string is of odd length  ` `    ``if` `length ``%` `2` `=``=` `1``:  ` `        ``return` `False` ` `  `    ``# To store the frequency of each  ` `    ``# character of the string  ` `    ``freq ``=` `[``0``] ``*` `26` ` `  `    ``# Update the frequencies of  ` `    ``# the characters  ` `    ``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` ` `  `    ``# No such character exists  ` `    ``return` `False` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"``:  ` ` `  `    ``string ``=` `"geeksforgeeks"` `    ``length ``=` `len``(string)  ` `    ``if` `isFrequencyEqual(string, length):  ` `        ``print``(``"Yes"``)  ` `    ``else``: ` `        ``print``(``"No"``)  ` ` `  `# This code is contributed by Rituraj Jain `

 `// C# implementation of the above approach. ` `using` `System; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function that returns true if some character  ` `    ``// exists in the given string whose frequency  ` `    ``// is equal to the sum frequencies of  ` `    ``// other characters of the string  ` `    ``static` `bool` `isFrequencyEqual(String str, ``int` `len) ` `    ``{ ` ` `  `        ``// If string is of odd length  ` `        ``if` `(len % 2 == 1)  ` `        ``{ ` `            ``return` `false``; ` `        ``} ` ` `  `        ``// To store the frequency of each  ` `        ``// character of the string  ` `        ``int` `i; ` `        ``int` `[]freq = ``new` `int``[26]; ` ` `  `        ``// Update the frequencies of the characters  ` `        ``for` `(i = 0; i < len; i++)  ` `        ``{ ` `            ``freq[str[i] - ``'a'``]++; ` `        ``} ` ` `  `        ``for` `(i = 0; i < 26; i++) ` `        ``{ ` `            ``if` `(freq[i] == len / 2)  ` `            ``{ ` `                ``return` `true``; ` `            ``} ` `        ``} ` ` `  `        ``// No such character exists  ` `        ``return` `false``; ` `    ``} ` ` `  `    ``// Driver code  ` `    ``public` `static` `void` `Main()  ` `    ``{ ` `        ``String str = ``"geeksforgeeks"``; ` `        ``int` `len = str.Length; ` `        ``if` `(isFrequencyEqual(str, len))  ` `        ``{ ` `            ``Console.WriteLine(``"Yes"``); ` `        ``}  ` `        ``else` `        ``{ ` `            ``Console.WriteLine(``"No"``); ` `        ``} ` `    ``} ` `} ` ` `  `/* This code contributed by PrinciRaj1992 */`

 ` `

Output:
```No
```

Time Complexity: O(len) where len is the length of the given string.

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.

Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :