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.
Input: arr1 = [“geeks”, “learn”, “for”, “egeks”, “ealrn”], arr2 = [“kgees”, “rof”, “nrael”]
Output: 2 1 2
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
Anagrams of arr2 (“edoc”) in arr1 “code” and “odce”.
Anagrams of arr2 (“wgor”) in arr1 “grow”.
Anagrams of arr2 (“ot”) in arr1 “to”
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:
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.
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- First string from the given array whose reverse is also present in the same array
- Count elements present in first array but not in second
- Count of elements A[i] such that A[i] + 1 is also present in the Array
- Count the factors of K present in the given Array
- Count pairs with average present in the same array
- Count subsequences in first string which are anagrams of the second string
- Count of strings in the first array which are smaller than every string in the second array
- Print all pairs of anagrams in a given array of strings
- Count words present in a string
- Count of number of given string in 2D character array
- Count of distinct substrings of a string using Suffix Array
- Probability of a key K present in array
- Sum of all palindrome numbers present in an Array
- Find elements which are present in first array and not in second
- Sum of all perfect numbers present in an array
- Sum of all mersenne numbers present in an array
- Sum of elements whose square root is present in the array
- Check if a key is present in every segment of size k in an array
- Program to find if two numbers and their AM and HM are present in an array using STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : Pawan_29