Ctrl K

Mismatches Between Codes

Medium
You are given an array codes of positive integers, all with the same number of digits. The **mismatch** of two codes is the number of positions where their digits differ. Return the total mismatch over all pairs of codes (each unordered pair of indices counts once). Checking every pair is O(n²); you need something faster.
Example 1
codes=[407, 417, 908] → 6

407 and 417 differ in one position, 407 and 908 in two, 417 and 908 in three. Total 1 + 2 + 3 = 6.

Example 2
codes=[55, 55, 55] → 0

All codes are identical, so there are no mismatches.

Example 3
codes=[3, 8] → 1
Constraints
  • 2 ≤ len(codes) ≤ 10⁵
  • 1 ≤ codes[i] < 10⁹
  • All numbers in codes have the same number of digits
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPairs Balanced by Their Mirrors MediumPhoto Frames of the Same Shape MediumPairs Off the Pace MediumNumbers and Their Mirrors MediumPrimes Along Grid Rays Medium
loading Python…
Input
codes=[407, 417, 908]
Expected
6

Run checks the first 3 tests in your browser. Submit checks all 104 tests on the server, hidden ones included.