title: Given a word and a text, return the count of the occurrences of anagrams of the word in the text
body: <p>For eg. word is <code>for</code> and the text is <code>forxxorfxdofr</code>, anagrams of <code>for</code> will be <code>ofr</code>, <code>orf</code>, <code>fro</code>, etc. So the answer would be <code>3</code> for this particular example.</p>
  
  <p>Here is what I came up with.</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;cstring&gt;
  
  using namespace std;
  
  int countAnagram (char *pattern, char *text)
  {
      int patternLength = strlen(pattern);
      int textLength = strlen(text);
  
      int dp1[256] = {0}, dp2[256] = {0}, i, j;
  
      for (i = 0; i &lt; patternLength; i++)
      {
          dp1[pattern[i]]++;
          dp2[text[i]]++;
      }
  
      int found = 0, temp = 0;
  
      for (i = 0; i &lt; 256; i++)
      {
          if (dp1[i]!=dp2[i])
          {
              temp = 1;
              break;
          }
      }
  
      if (temp == 0)
          found++;
  
  
      for (i = 0; i &lt; textLength - patternLength; i++)
      {
          temp = 0;
          dp2[text[i]]--;
          dp2[text[i+patternLength]]++;
          for (j = 0; j &lt; 256; j++)
          {
              if (dp1[j]!=dp2[j])
              {
                  temp = 1;
                  break;
              }
          }
          if (temp == 0)
              found++;
      }
      return found;
  }
  
  
  int main()
  {
      char pattern[] = "for";
      char text[] = "ofrghofrof";
  
      cout &lt;&lt; countAnagram(pattern, text);
  
  }
  </code></pre>
  
  <p>Does there exist a faster algorithm for the said problem?</p>