Skip to content

Latest commit

 

History

History
36 lines (26 loc) · 527 Bytes

Hamming Distance.md

File metadata and controls

36 lines (26 loc) · 527 Bytes

Algorithm

XOR has a value of 1 only when 2 bits are different:

XOR

a  b    a ^ b
-------------
0  0      0
0  1      1
1  0      1
1  1      0
  1. Use XOR as x ^ y
  2. The number of bits set as 1 in x ^ y is our solution

Solution

class Solution {
    public int hammingDistance(int x, int y) {
        return Integer.bitCount(x ^ y);
    }
}

Time/Space Complexity

  • Time Complexity: O(1)
  • Space Complexity: O(1)

Links