title: Python collections.Counter efficiency
body: <p>I am using the following code to implement a function which finds all anagrams of string p in a string s. </p>
  
  <pre><code>class Solution(object):
      def findAnagrams(self, s, p):
          """
          :type s: str
          :type p: str
          :rtype: List[int]
          """
          ans = list()
          pcnt = collections.Counter(p)
          for i in range(len(s)):
              if collections.Counter(s[i:i+len(p)]) == pcnt:
                  ans.append(i)
          return ans
  </code></pre>
  
  <p>when running on large length input string s, it gives me "time exceeds limit" error in the online code testing system. However the following code will work with no such issue:</p>
  
  <pre><code>class Solution(object):
      def findAnagrams(self, s, p):
          """
          :type s: str
          :type p: str
          :rtype: List[int]
          """
          ls, lp = len(s), len(p)
          cp = collections.Counter(p)
          cs = collections.Counter()
          ans = []
          for i in range(ls):
              cs[s[i]] += 1
              if i &gt;= lp:
                  cs[s[i - lp]] -= 1
                  if cs[s[i - lp]] == 0:
                      del cs[s[i - lp]]
              if cs == cp:
                  ans.append(i - lp + 1)
          return ans
  </code></pre>
  
  <p>Can I know why? It seems both solution uses two Counters of maximum size of len(p)?</p>