Character whose frequency is equal to the sum of frequencies of other characters of the given string


Introduction

A C++ string is a stream of alphanumeric characters. A string has the following properties −

  • A string is composed of a fixed set of characters

  • The strings positions start by default from the 0th index

  • Frequency of any character is the number of times it occurs in the string. The frequency of any any character can range from 0 , if it doesn’t occur to the length of the string.

In this article, we are going to develop a code that takes as input a string, and checks whether the frequency of any character is equivalent to the summation of frequencies of all the other characters in the string. Let us look at the following example to understand the topic better

Sample Example

Example 1 − str − “@!ab@!”

Output − True

For instance, the following example string, contains special characters also, where, the corresponding frequencies of each character are as follows −

@ = 4

! = 2

a = 1

b = 1

So, the following string has the following applicable property

freq(@) = freq(!) + freq(a)+freq(b)

4 = 2 + 1 + 1

In this article, we will create a solution to compute the number of times each character in the string occurs, and check further if there is any character with the required frequency count

Syntax

str.length()

length()

The length() method in C++ is used to compute the number of characters in the string.

Algorithm

  • An input string, str is accepted

  • An array of 26 alphabets is created to store the frequency of the character. It is initialized with the count 0, specified by freq array

  • The length of the string is computed using the length() method, denoted by len

  • In case, the length of the string is odd, false flag is returned

  • Each time the character at ith position is extracted

  • The frequency of this character is incremented by 1.

  • After computing the entire length of the string, the frequency array is checked

  • In case the frequency of a characters is equivalent to the summation of frequencies of other characters, a boolean flag value true is returned.

Example

The following C++ code snippet is used to check from the given input string, whether there is any character that occurs equal to the sum of the frequencies of all the characters respectively −

//including the required libraries
#include <bits/stdc++.h>
using namespace std;
 
//function to check if the frequency of occurrence of data is equivalent to other characters' frequency
bool charwithequalFreq(string str) {
   //storing the frequency of characters 
   int freq[26] = { 0 };
   //length of string 
   int len = str.length();
   //if the length of the string is odd 
   if (len % 2 == 1)
      return false;
 
   // Update the frequencies of the characters
   for (int i = 0; i < len; i++){
       char ch = str[i];
       freq[ch - 'a']+=1;
   }
        
 
   for (int i = 0; i < 26; i++)
      if (freq[i] == len / 2)
      {       
         cout<<"Holds true for character "<<(char)(i+'a') <<"\n";
         return true;
      }
   
   //none of the cases hold true
   return false;
}
//calling the frequency method
int main() {
    
   //input string
   string str = "tweeet";
 
   cout<< "Input String : "<<str<<"\n";
   //check the frquency 
   bool res = charwithequalFreq(str);
   if(!res){
      cout<<"There is no such character";
   }
   return 0;
}

Output

Input String : tweeet
Holds true for character e

Conclusion

The character positions in a C++ string start from by default the 0th index. A string is a dynamic length storage structure where the characters can be appended easily any number of times. Every character in a C++ string is associated with a count, indicated by its frequency. The map data structure is very useful where in each key is associated with a definite value.

Updated on: 31-Jul-2023

60 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements