DatabricksOA #26
Count Distinct Pairs
估计中等可在线判题
You are given an array of positive integers. An index pair i<j is valid if, after swapping the digits at two positions of the decimal representation of fruits[j] zero times or once, the resulting integer equals fruits[i]. Count the valid index pairs. Zero swaps are allowed, so equal values also count. The digit string after swapping is interpreted as an ordinary decimal integer; leading zeros are not kept.
输入格式
The first line contains an integer n (1≤n≤10000); the second line contains n positive integers fruits[i] (1≤fruits[i]≤10^9).
输出格式
Output the number of valid index pairs.
样例 1
输入:
7
1 23 156 1650 651 165 32
输出:
3
样例 2
输入:
3
123 321 123
输出:
3
样例 3
输入:
4
1 1000 10 100
输出:
4
样例说明
For each right-end value, enumerate the integers reachable with zero or one swap, and look up how many times each occurred earlier. Do not treat all numbers with the same digits as mutually reachable; one swap can change only two positions.