# Count of anagrams of each string in an array present in another array

Given two arrays arr1[] and arr2[] consisting of strings, the task is to print the count of anagrams of every string in arr2[] that are present in in arr1[].

Examples:

Input: arr1[] = [“geeks”, “learn”, “for”, “egeks”, “ealrn”], arr2[] = [“kgees”, “rof”, “nrael”]
Output: 2 1 2
Explanation:
Anagrams of arr2 (“kgees”) in arr1 : “geeks” and “egeks”.
Anagrams of arr2 (“rof”) in arr1 : “for”.
Anagrams of arr2 (“nrael”) in arr1 : “learn” and “ealrn”.

Input: arr1[] = [“code”, “to”, “grow”, “odce”], arr2[] = [“edoc”, “wgor”, “ot”]
Output: 2 1 1
Explanation:
Anagrams of arr2 (“edoc”) in arr1 “code” and “odce”.
Anagrams of arr2 (“wgor”) in arr1 “grow”.
Anagrams of arr2 (“ot”) in arr1 “to”

Approach:
To solve the problem, the idea is to use frequency-counting with the help of HashMap. Store the frequencies of every string in arr1[] in hashmap in their sorted form. Traverse arr2[], sort strings in arr2[], and print their respective frequencies in HashMap.

Below is the implementation of the above approach:

## C++

 `// C++ Program to count the ` `// number of anagrams of ` `// each string in a given ` `// array present in ` `// another array ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to return the ` `// count of anagrams ` `void` `count(string arr1[], ` `           ``string arr2[], ` `           ``int` `n, ``int` `m) ` `{ ` `    ``// Store the frequencies ` `    ``// of strings in arr1[] ` `    ``map freq; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// Sort the string ` `        ``sort(arr1[i].begin(), ` `             ``arr1[i].end()); ` ` `  `        ``// Increase its frequency ` `        ``// in the map ` `        ``freq[arr1[i]]++; ` `    ``} ` ` `  `    ``for` `(``int` `i = 0; i < m; i++) { ` ` `  `        ``// Sort the string ` `        ``sort(arr2[i].begin(), ` `             ``arr2[i].end()); ` ` `  `        ``// Display its anagrams ` `        ``// in arr1[] ` `        ``cout << freq[arr2[i]] ` `             ``<< ``" "``; ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` ` `  `    ``string arr1[] = { ``"geeks"``, ``"learn"``, ` `                      ``"for"``, ``"egeks"``, ` `                      ``"ealrn"` `}; ` `    ``int` `n = ``sizeof``(arr1) ` `            ``/ ``sizeof``(string); ` ` `  `    ``string arr2[] = { ``"kgees"``, ``"rof"``, ` `                      ``"nrael"` `}; ` `    ``int` `m = ``sizeof``(arr2) ` `            ``/ ``sizeof``(string); ` ` `  `    ``count(arr1, arr2, n, m); ` `} `

Output:

```2 1 2
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.