FastPrepCount Equal or One-Swap Number Pairs

Count Equal or One-Swap Number Pairs

Susquehanna International Group (SIG) logoSusquehanna International Group (SIG)● MediumFULLTIMEOA

Problem statement

You are given an array of positive integers numbers. Count pairs of indices (i,j) with i < j such that the two values are equal, or one value can become the other by swapping exactly one pair of decimal digit positions in that one value.

Use the ordinary decimal representation of each number. A swap that places 0 in the leading position is invalid, so valid transformed numbers keep the same number of digits. You may not make one swap in each value.

Count each pair of indices once, even if several possible swaps produce the same value. Equal numbers at different indices count without a swap. Return the number of qualifying index pairs.

Function

countOneSwapPairs(numbers: int[]) → long

Examples

Example 1

numbers = [1,23,156,1650,651,165,32]return = 3

The qualifying value pairs are 23/32, 156/651 and 156/165. Transforming 1650 into 0651 is invalid because it creates a leading zero.

Example 2

numbers = [123,321,123]return = 3

All three index pairs qualify: each 123 matches321 by one swap, and the two 123 values are equal.

Example 3

numbers = [1,10,100]return = 0

Different digit lengths cannot be reconciled by a legal swap, so the count is 0.

Constraints

  • 1 <= numbers.length <= 100000.
  • 1 <= numbers[i] <= 10^9.
  • Return a 64-bit integer count.

More Susquehanna International Group (SIG) problems

See Susquehanna International Group (SIG) hiring insights
Saved
public long countOneSwapPairs(int[] numbers) {
    // Write your code here.
}
numbers[1,23,156,1650,651,165,32]
expected3
Checking account…