# Count of total anagram substrings in C++

C++Server Side ProgrammingProgramming

We are given a string str[] as input. The goal is to count the number of anagram substrings present in str[]. Two strings are anagrams of each other if they contain the same number of characters and all characters occur in both. The order of characters can be different.

“abc” is an anagram of “cba”, “bca” etc.

Let us understand with examples.

Input − str[] = “abccb”

Output − Count of total anagram substrings are − 4

Explanation − Anagrams are − (b,b), (c,c), (bc,cb), (bcc,ccb)

Input − str = “aaa”

Output − Count of total anagram substrings are − 4

Explanation − Anagrams are − (a,a), (a,a), (a,a), (aa,aa)

## Approach used in the below program is as follows

We are taking a map containing vectors with frequencies of english alphabets in substring and count of such substrings in array. In map<vector<int>, int> mp_vec; vector<int> vec(MAX, 0) will store frequency of all 26 alphabets in current substring. And mapped integers will be count of such substrings with the same frequency vector. For each substring if count is x for anagrams. Then total anagram pairs will be x*(x-1)/2.

• Take string str[] as a character array.

• Function anagram_substring(string str, int length) takes the string and returns the count of total anagram substrings.

• Take the initial count as 0.

• Take a map, map<vector<int>, int> mp_vec;

• Traverse str[] using two for loops from i=0 to i<length and j=i to j<length.

• For each substring str[i to j]. vector<int> vec(MAX, 0); will contain counts of English alphabets in it.

• Take current character in c as str[j]. Take its integer value by temp=c-’a’.

• Update frequency by vec[temp]++.

• Increase count corresponding to this frequency vector using mp_vec[vec]++.

• Now traverse map containing all frequency vector and aggregate substring count using for loop from iterator it=mp_vec.begin() to it != mp_vec.end().

• For each count as it->second, add ((last) * (last-1))/2 to count for all pairs of anagrams

• At the end we will have a count of all anagrams.

• Return count as result.

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
#define MAX 26
int anagram_substring(string str, int length){
int count = 0;
map<vector<int>, int> mp_vec;
for (int i=0; i<length; i++){
vector<int> vec(MAX, 0);
for (int j=i; j<length; j++){
char c = str[j];
char temp = c - 'a';
vec[temp]++;
mp_vec[vec]++;
}
}
for (auto it = mp_vec.begin(); it != mp_vec.end(); it++){
int last = it->second;
count += ((last) * (last-1))/2;
}
return count;
}
int main(){
string str = "TP";
int length = str.length();
cout<<"Count of total anagram substrings are: "<<anagram_substring(str, length) << endl;
return 0;
}

## Output

If we run the above code it will generate the following output −

Count of total anagram substrings are: 3
Published on 03-Dec-2020 07:15:32