
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count of total anagram substrings in C++
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
#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
- Related Articles
- Count Binary Substrings in C++
- Program to count number of palindromic substrings in Python
- Program to count number of homogenous substrings in Python
- Find the count of substrings in alphabetic order in C++
- Count of Palindromic substrings in an Index range in C++
- Program to count number of distinct substrings in s in Python
- Count all Prime Length Palindromic Substrings in C++
- Program to find total similarities of a string and its substrings in Python
- Count number of substrings with exactly k distinct characters in C++
- Count Unique Characters of All Substrings of a Given String in C++
- Count of substrings of a binary string containing K ones in C++
- Maximum count of substrings of length K consisting of same characters in C++
- C++ code to count number of even substrings of numeric string
- Count number of substrings with numeric value greater than X in C++
- Count Substrings with equal number of 0s, 1s and 2s in C++
