LeetCode solutions

2953. Count Pairs of Points With Distance k

My accepted Python solution to LeetCode problem 2953, Count Pairs of Points With Distance k, running in 2687ms.

  • Difficulty: Medium
  • Python
  • Runtime 2687ms
  • Memory 32.3MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 2687ms, memory 32.3MB, accepted 2026-01-01.

python
class Solution:
    def countPairs(self, coordinates: List[List[int]], k: int) -> int:
        count = Counter()
        result = 0
        
        for x, y in coordinates:
            for i in range(k + 1):
                x2 = x ^ i
                y2 = y ^ (k - i)
                result += count[(x2, y2)]
            count[(x, y)] += 1
        
        return result

Source