Problem · Hash Table

Count Distinct Pairs

Learn this problem
MediumDatabricks logoDatabricksINTERNOA

Problem 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[]) → int

Examples

Example 1

fruits = [1, 23, 156, 4738, 321, 72992, 231, 651, 32]return = 3

The 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 <= 10000
  • 1 <= fruits[i] <= 1000000000
  • The answer fits a 32-bit signed integer.

More Databricks problems

drafts saved locally
public int ddCountPairs(int[] fruits) {
    // write your code here
}
fruits[1, 23, 156, 4738, 321, 72992, 231, 651, 32]
expected3
checking account