SDE & MLE Prep

461. Hamming Distance

Problem

Return the Hamming distance between x and y: the number of bit positions in which they differ.

Example: x = 1, y = 4 → 2 (001 vs 100)

Differing bits = set bits of x ^ y.

Complexity: O(1).

class Solution:
    def hammingDistance(self, x: int, y: int) -> int:
        return bin(x ^ y).count("1")