# A. Number of Common Factors (opens new window)

Check every integer from 1 to min(a, b) inclusive.

DETAILS
class Solution:
    def commonFactors(self, a: int, b: int) -> int:
        return sum((a % x == 0) and (b % x == 0) for x in range(1, min(a, b) + 1))

# B. Maximum Sum of an Hourglass (opens new window)

Brute Force.

DETAILS
class Solution:
    def maxSum(self, A: List[List[int]]) -> int:
        m, n = len(A), len(A[0])
        f = lambda x, y: A[x][y] + sum(A[x - 1][y - 1:y + 2]) + sum(A[x + 1][y - 1:y + 2])
        return max(f(x, y) for x in range(1, m - 1) for y in range(1, n - 1))

# C. Minimize XOR (opens new window)

Offset bits int nums1 from high to low, then revert the rest 0 to 1 from low to high.

DETAILS
class Solution:
    def minimizeXor(self, num1: int, num2: int) -> int:
        a, b = num1.bit_count(), num2.bit_count()
        res = num1
        for i in range(32):
            if a > b and (1 << i) & num1:
                res ^= 1 << i
                a -= 1
            if a < b and (1 << i) & num1 == 0:
                res ^= 1 << i
                a += 1
        return res

# D. Maximum Deletions on a String (opens new window)

2D-dp, LCS.

DETAILS
class Solution:
    def deleteString(self, s: str) -> int:
        n = len(s)
        if len(set(s)) == 1:
            return n
        dp = [1] * n
        for i in range(n - 2, -1, -1):
            for l in range(1, (n - i) // 2 + 1):
                if s[i : i + l] == s[i + l : i + 2 * l]:
                    dp[i] = max(dp[i], 1 + dp[i + l])
        return dp[0]