Count substrings with same first and last characters in C++

We are given a string str. The goal is to count the number of substrings in str that have the same starting and ending character. For example, if input is “baca” substrings will be “b”, “a”, “c”, “a”, “aca”. Total 5.
Let us understand with examples.
Input − str=”abaefgf”
Output −Count of substrings with same first and last characters are: 9
Explanation − Substrings will be
“a”, “b”, “a”, “e” ,”f”, “g”, “f”, “aba”, “fgf”. Total 9.
Input − str=”abcdef”
Output −Count of substrings with same first and last characters are: 6
Explanation − Substrings will be −
“a” , “b” , “c”, “d”, “e” ,”f”. Total 6
There can be multiple approaches to solve the given problem i.e. naive approach and efficient approach. So let’s first look at the naive approach. We will pass substrings of every length to a function check(). If that substring starts and ends with the same character then increment the count.
Take string str. Calculate the length as str.size().
Take string str. Calculate the length as str.size().
Function check(string str) takes substring str and checks if it’s first and last character are the same. ( str[0]==str[length-1] ). If true, return 1.
Function check(string str) takes substring str and checks if it’s first and last character are the same. ( str[0]==str[length-1] ). If true, return 1.
Function check_Start_End(string str, int length) takes str and its length as input and returns the count of substrings of str which start and end with the same character.
Function check_Start_End(string str, int length) takes str and its length as input and returns the count of substrings of str which start and end with the same character.
Take the initial count as 0.
Take the initial count as 0.
Traverse str using two for loops. From i=0 to i<length and inner j=1 to j=length-1.
Traverse str using two for loops. From i=0 to i<length and inner j=1 to j=length-1.
We will get all substrings using substr(i,j) of all lengths. Pass each substring to check(). If it returns 1, increment count.
We will get all substrings using substr(i,j) of all lengths. Pass each substring to check(). If it returns 1, increment count.
At the end of both for loops count will have a number of substrings of str with the same start and end character.
At the end of both for loops count will have a number of substrings of str with the same start and end character.
Return count as the desired result.
Return count as the desired result.
As we can see, that answer depends on the frequency of each character in the original string.
For example in “bacba” the frequency of “b” is 2 and substrings including “b” are “b”, “bacb” and “b”.
Which is 2+1C2=3. We will first check the frequency of each character and add (freq of char+1)C2.
Take string str. Calculate the length as str.size().
Take string str. Calculate the length as str.size().
Function check_Start_End(string str, int length) takes str and its length as input and returns the count of substrings of str which start and end with the same character.
Function check_Start_End(string str, int length) takes str and its length as input and returns the count of substrings of str which start and end with the same character.
Take initial count-0.
Take initial count-0.
Take an array arr[26] to store the frequency of each character.
Take an array arr[26] to store the frequency of each character.
Traverse string and store frequency arr[str[i]=’a’]++.
Traverse string and store frequency arr[str[i]=’a’]++.
Traverse frequency array arr[26] and for each frequency arr[i] add arr[i]*(arr[i]+1)/2. To count.
Traverse frequency array arr[26] and for each frequency arr[i] add arr[i]*(arr[i]+1)/2. To count.
In the end return count as result.
In the end return count as result.
 Live Demo
#include <bits/stdc++.h>
using namespace std;
int check(string str){
   int length = str.length();
   if(str[0] == str[length-1]){
      return 1;
   }
}
int check_Start_End(string str, int length){
   int count = 0;
   for (int i = 0; i < length; i++){
      for (int j = 1; j <= length-i; j++){
         if (check(str.substr(i, j))){
            count++;
         }
      }
   }
   return count;
}
int main(){
   string str = "bcbdedfef";
   int length = str.length();
   cout<<"Count of substrings with same first and last characters are: "<<check_Start_End(str, length);
   return 0;
}
If we run the above code it will generate the following output −
Count of substrings with same first and last characters are: 13
 Live Demo
#include <bits/stdc++.h>
using namespace std;
#define maximum 26
int check_Start_End(string str, int length){
   int count = 0;
   int arr[maximum] = {0};
   for(int i=0; i<length; i++){
      arr[str[i] - 'a']++;
   }
   for (int i=0; i<maximum; i++){
      count = count + (arr[i]*(arr[i]+1)/2);
   }
   return count;
}
int main(){
   string str = "bcbdedfef";
   int length = str.length();
   cout<<"Count of substrings with same first and last characters are: "<<check_Start_End(str,
length);
   return 0;
}
If we run the above code it will generate the following output −
Count of substrings with same first and last characters are: 13