コード例 #1
0
 def canReorderDoubled(self, A):
     c = Collection.Counter(A)
     for x in sorted(c, key=abs):
         if c[x] > c[2 * x]:
             return False
         c[2 * x] -= c[x]
     return True
コード例 #2
0
 def customSortString(self, S: str, T: str) -> str:
     ans, cnt = [], Collection.Counter(T)  # count each char in T.
     for c in S:
         if cnt[c]:
             ans.extend(
                 c * cnt.pop(c)
             )  # sort chars both in T and S by the order of S.
     for c, v in cnt.items():
         ans.extend(c * v)  # group chars in T but not in S.
     return "".join(ans)
コード例 #3
0
 def minWindow(self, s, t):
     need, missing = Collection.Counter(t), len(t)
     i = I = J = 0
     for j, c in enumerate(s, 1):
         missing -= need[c] > 0
         need[c] -= 1
         if not missing:
             while i < j and need[s[i]] < 0:
                 need[s[i]] += 1
                 i += 1
             if not J or j - i <= J - I:
                 I, J = i, j
     return s[I:J]
コード例 #4
0
    def hasGroupsSizeX(self, deck: List[int]) -> bool:
        gcd = 0
        # Find frequency of each card number
        freq = Collection.Counter(deck)

        # Calculate GCD of freq dictionary 
        for (key, value) in freq.items():
            if gcd == 0:
                gcd = value
            else:
                gcd = math.gcd(gcd, value)

        if gcd == 1:
            return False
        return True
コード例 #5
0
 def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
     start = end = left = 0
     need = Collection.Counter(t)
コード例 #6
0
 def maxNumberOfBalloons(self, text: str) -> int:
     cnt = Collection.Counter(text)
     cntBalloon = collections.Counter("balloon")
     return min([cnt[c] // cntBalloon[c] for c in cntBalloon])