Count Pairs Differing in One Digit
Problem statement
You are given an array of positive integers numbers. Count index pairs i < j whose ordinary decimal representations have the same length and differ at exactly one aligned digit position.
For this exercise, assume decimal representations have no leading zeros. Different occurrences of an equal value are different array elements, but two equal values differ at zero positions and therefore do not form a qualifying pair.
Return the number of qualifying index pairs as a signed 64-bit integer.
Function
countOneDigitDifferencePairs(numbers: int[]) → longExamples
Example 1
numbers = [12,13,22,12]return = 4The qualifying pairs connect either occurrence of 12 with 13 or 22. The equal values do not qualify.
Example 2
numbers = [1,2,10,20]return = 2The pairs are 1 with 2 and 10 with 20. Different decimal lengths are not compared.
Example 3
numbers = [111,121,131,222]return = 3The first three values differ only at their middle digit, producing three pairs. 222 differs at more than one position from each.
Constraints
0 <= numbers.length <= 100000.1 <= numbers[i] <= 10^9.