Problem · Hash Table

Count Distinct Pairs

Learn this problem
MediumGoogle logoGoogleONSITE INTERVIEW
See Google hiring insights

Problem statement

Given an array of non-negative integers nums, count the index pairs (i, j) such that 0 <= i < j < nums.length and nums[i] can be obtained from nums[j] by swapping at most one pair of decimal digit positions.

Making no swap is allowed, so equal values form a valid pair. If a swap places one or more zeros at the front of the decimal representation, interpret the result as an integer; leading zeros do not change its value.

Function

countDistinctPairs(nums: int[]) → long

Examples

Example 1

nums = [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. In each pair, the later value becomes the earlier value after one digit-position swap.

Constraints

  • Each value in nums is a non-negative integer.
  • Values use their usual decimal representation; leading zeros created by a swap do not change the resulting integer value.

More Google problems

drafts saved locally
public long countDistinctPairs(int[] nums) {
    // Write your code here.
}
nums[1, 23, 156, 4738, 321, 72992, 231, 651, 32]
expected3
checking account