Problem · Hash Table

Count Distinct Pairs

MediumDatabricksINTERNOA

There is an array of fruits. Your goal is to calculate the amount of distinct pairs (x, y) so that 0 <= x < y < length of fruits. Note that fruits[x] could be gotten from fruits[y] by swapping no more than 2 digits of fruits[y] :)

Note again:: One might not make swapping to any digits at all, so, if x < y and fruits[x] = fruits[y], such pair should be counted.

Examples
01 · Example 1
fruits = [1, 23, 156, 1650, 651, 165, 32]
return = 3
- fruist[1] = 23 could be gotten from fruits[6] = 32 just by swapping its only 2 digits :) - fruist[2] = 156 could be gotten from fruits[4] = 651 just by swapping six and one :)) - fruist[2] = 156 could be gotten from fruits[5] = 165 just by swapping six & five :))
02 · Example 2
fruits = [123, 321, 123]
return = 3
- fruits[1] = 321 could be gotten from fruits[0] = 123, swapping one & three - fruits[2] = 123 could be gotten from fruits[0] = 123, swapping nothing - fruits[2] = 123 could be gotten from fruits[1] = 321, swapping three & one
Constraints
  • 1 <= fruits.length <= 104
  • 1 <= numbers[i] <= 109
  • More Databricks problems
    drafts saved locally
    public int ddCountPairs(int[] fruits) {
        // write your code here
    }
    
    fruits[1, 23, 156, 1650, 651, 165, 32]
    expected3
    checking account