Problem · Hash Table
Count Distinct Pairs
Learn this problemProblem statement
Given an array of positive integers fruits, count the index pairs (x, y) such that 0 <= x < y < fruits.length and fruits[x] can be obtained from fruits[y] by swapping at most one pair of decimal digit positions. Making no swap is allowed, so equal values form a valid pair.
Function
ddCountPairs(fruits: int[]) → intExamples
Example 1
fruits = [1, 23, 156, 4738, 321, 72992, 231, 651, 32]return = 3The valid pairs are 23 with 32, 156 with 651, and 321 with 231. Each later value becomes the earlier value after one digit-position swap.
Constraints
1 <= fruits.length <= 100001 <= fruits[i] <= 1000000000- The answer fits a 32-bit signed integer.