# Count of total anagram substrings

Given a string of lower alphabet characters, count total substring of this string which are anagram to each other.

**Examples:**

Input : str = “xyyx” Output : 4 Total substrings of this string which are anagram to each other are 4 which can be enumerated as, {“x”, “x”}, {"y", "y"}, {“xy”, “yx”}, {“xyy”, “yyx”} Input : str = "geeg" Output : 4

The idea is to create a map. We use character frequencies as keys and corresponding counts as values. We can solve this problem by iterating over all substrings and counting frequencies of characters in every substring. We can update frequencies of characters while looping over substrings i.e. there won’t be an extra loop for counting frequency of characters.

In below code, a map of key ‘vector type’ and value ‘int type’ is taken for storing occurrence of ‘frequency array of length 26’ of substring characters. Once occurrence ‘o’ of each frequency array is stored, total anagrams will be the sum of o*(o-1)/2 for all different frequency arrays because if a particular substring has ‘o’ anagrams in string total o*(o-1)/2 anagram pairs can be formed.

Below is the implementation of above idea.

## C++

`// C++ program to count total anagram ` `// substring of a string ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Total number of lowercase characters ` `#define MAX_CHAR 26 ` ` ` `// Utility method to return integer index ` `// of character 'c' ` `int` `toNum(` `char` `c) ` `{ ` ` ` `return` `(c - ` `'a'` `); ` `} ` ` ` `// Returns count of total number of anagram ` `// substrings of string str ` `int` `countOfAnagramSubstring(string str) ` `{ ` ` ` `int` `N = str.length(); ` ` ` ` ` `// To store counts of substrings with given ` ` ` `// set of frequencies. ` ` ` `map<vector<` `int` `>, ` `int` `> mp; ` ` ` ` ` `// loop for starting index of substring ` ` ` `for` `(` `int` `i=0; i<N; i++) ` ` ` `{ ` ` ` `vector<` `int` `> freq(MAX_CHAR, 0); ` ` ` ` ` `// loop for length of substring ` ` ` `for` `(` `int` `j=i; j<N; j++) ` ` ` `{ ` ` ` `// update freq array of current ` ` ` `// substring ` ` ` `freq[toNum(str[j])]++; ` ` ` ` ` `// increase count corresponding ` ` ` `// to this freq array ` ` ` `mp[freq]++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// loop over all different freq array and ` ` ` `// aggregate substring count ` ` ` `int` `result = 0; ` ` ` `for` `(` `auto` `it=mp.begin(); it!=mp.end(); it++) ` ` ` `{ ` ` ` `int` `freq = it->second; ` ` ` `result += ((freq) * (freq-1))/2; ` ` ` `} ` ` ` `return` `result; ` `} ` ` ` `// Driver code to test above methods ` `int` `main() ` `{ ` ` ` `string str = ` `"xyyx"` `; ` ` ` `cout << countOfAnagramSubstring(str) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count total anagram ` `# substring of a string ` `def` `countOfAnagramSubstring(s): ` ` ` ` ` `# Returns total number of anagram ` ` ` `# substrings in s ` ` ` `n ` `=` `len` `(s) ` ` ` `mp ` `=` `dict` `() ` ` ` ` ` `# loop for length of substring ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `sb ` `=` `'' ` ` ` `for` `j ` `in` `range` `(i, n): ` ` ` `sb ` `=` `''.join(` `sorted` `(sb ` `+` `s[j])) ` ` ` `mp[sb] ` `=` `mp.get(sb, ` `0` `) ` ` ` ` ` `# increase count corresponding ` ` ` `# to this dict array ` ` ` `mp[sb] ` `+` `=` `1` ` ` ` ` `anas ` `=` `0` ` ` ` ` `# loop over all different dictionary ` ` ` `# items and aggregate substring count ` ` ` `for` `k, v ` `in` `mp.items(): ` ` ` `anas ` `+` `=` `(v` `*` `(v` `-` `1` `))` `/` `/` `2` ` ` `return` `anas ` ` ` `# Driver Code ` `s ` `=` `"xyyx"` `print` `(countOfAnagramSubstring(s)) ` ` ` `# This code is contributed by fgaim ` |

*chevron_right*

*filter_none*

**Output:**

4

This article is contributed by **Utkarsh Trivedi**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Count substrings that contain all vowels | SET 2
- Count substrings with same first and last characters
- Number of substrings with count of each character as k
- Count Substrings with equal number of 0s, 1s and 2s
- Count of substrings which contains a given character K times
- Count of substrings of a binary string containing K ones
- Find the count of substrings in alphabetic order
- Count substrings with each character occurring at most k times
- Recursive solution to count substrings with same first and last characters
- Count all Prime Length Palindromic Substrings
- Count number of distinct substrings of a given length
- Count number of substrings with exactly k distinct characters
- Count of Palindromic substrings in an Index range
- Count number of substrings with numeric value greater than X
- Count distinct substrings that contain some characters at most k times