Related Articles
Count Occurrences of Anagrams
• Difficulty Level : Medium
• Last Updated : 11 Jun, 2020

Given a word and a text, return the count of the occurrences of anagrams of the word in the text(For eg: anagrams of word for are for, ofr, rof etc.))

Examples:

```Input : forxxorfxdofr
for
Output : 3
Explanation : Anagrams of the word for - for, orf,
ofr appear in the text and hence the count is 3.

Input : aabaabaa
aaba
Output : 4
Explanation : Anagrams of the word aaba - aaba,
abaa each appear twice in the text and hence the
count is 4.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A simple approach is to traverse from start of the string considering substrings of length equal to the length of the given word and then check if this substring has all the characters of word.

 `// A Simple Java program to count anagrams of a ` `// pattern in a text. ` `import` `java.io.*; ` `import` `java.util.*; ` ` `  `public` `class` `GFG { ` ` `  `    ``// Function to find if two strings are equal ` `    ``static` `boolean` `araAnagram(String s1, ` `                              ``String s2) ` `    ``{ ` `        ``// converting strings to char arrays ` `        ``char``[] ch1 = s1.toCharArray(); ` `        ``char``[] ch2 = s2.toCharArray(); ` ` `  `        ``// sorting both char arrays ` `        ``Arrays.sort(ch1); ` `        ``Arrays.sort(ch2); ` ` `  `        ``// Check for equality of strings ` `        ``if` `(Arrays.equals(ch1, ch2)) ` `            ``return` `true``; ` `        ``else` `            ``return` `false``; ` `    ``} ` ` `  `    ``static` `int` `countAnagrams(String text, String word) ` `    ``{ ` `        ``int` `N = text.length(); ` `        ``int` `n = word.length(); ` ` `  `        ``// Initialize result ` `        ``int` `res = ``0``; ` ` `  `        ``for` `(``int` `i = ``0``; i <= N - n; i++) { ` ` `  `            ``String s = text.substring(i, i + n); ` ` `  `            ``// Check if the word and substring are ` `            ``// anagram of each other. ` `            ``if` `(araAnagram(word, s)) ` `                ``res++; ` `        ``} ` `     `  `        ``return` `res; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``String text = ``"forxxorfxdofr"``; ` `        ``String word = ``"for"``; ` `        ``System.out.print(countAnagrams(text, word)); ` `    ``} ` `} `

Output:

```3
```

An Efficient Solution is to use count array to check for anagrams, we can construct current count window from previous window in O(1) time using sliding window concept.

 `// An efficient Java program to count anagrams of a ` `// pattern in a text. ` `import` `java.io.*; ` `import` `java.util.*; ` ` `  `public` `class` `GFG { ` `    ``final` `static` `int` `MAX_CHAR = ``256` ` `  `    ``// Function to find if two strings are equal ` `    ``static` `boolean` `isCountZero(``int``[] count) ` `    ``{ ` `        ``for` `(``int` `i = ``0``; i < MAX_CHAR; i++) ` `            ``if` `(count[i] != ``0``) ` `                ``return` `false``; ` `        ``return` `true``; ` `    ``} ` ` `  `    ``static` `int` `countAnagrams(String text, String word) ` `    ``{ ` `        ``int` `N = text.length(); ` `        ``int` `n = word.length(); ` ` `  `        ``// Check for first window. The idea is to ` `        ``// use single count array to match counts ` `        ``int``[] count = ``new` `int``[MAX_CHAR]; ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `            ``count[word.charAt(i)]++; ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `            ``count[text.charAt(i)]--; ` ` `  `        ``// If first window itself is anagram ` `        ``int` `res = ``0``; ` `        ``if` `(isCountZero(count)) ` `            ``res++; ` ` `  `        ``for` `(``int` `i = n; i < N; i++) { ` ` `  `            ``// Add last character of current ` `            ``// window ` `            ``count[text.charAt(i)]--; ` ` `  `            ``// Remove first character of previous ` `            ``// window. ` `            ``count[text.charAt(i - n)]++; ` ` `  `            ``// If count array is 0, we found an ` `            ``// anagram. ` `            ``if` `(isCountZero(count)) ` `                ``res++; ` `        ``} ` `        ``return` `res; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``String text = ``"forxxorfxdofr"``; ` `        ``String word = ``"for"``; ` `        ``System.out.print(countAnagrams(text, word)); ` `    ``} ` `} `

Output:

```3
```

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
Recommended Articles
Page :