cswork / OAOpen practice workspace

DatabricksOA #1

Count Pairs with One Digit Swap

Estimated MediumStatement and editorial — judging is being prepared

Topics (AI-labelled)HashingStrings

You are given an array of integers numbers. Your task is to count the number of distinct pairs of indices (i, j) (where i < j) such that numbers[i] can be obtained from numbers[j] by swapping exactly two digits in their decimal representation. Both numbers must have the same number of digits to be considered.

Example: numbers = [1, 23, 156, 1650, 651, 165, 32] returns 3 (pairs: (23, 32), (156, 651), (156, 165)).

Constraints

1 ≤ numbers.length ≤ 10⁴, 1 ≤ numbers[i] ≤ 10⁹.

Editorial

Loading solution…