Problem · Array

Count Distinct Swappable Digit Pairs

Learn this problem
MediumZipRecruiter logoZipRecruiterINTERNOA

Problem statement

Checkout the source image below for the original problem statement :)

Once upon a time, there was a mystical array filled with a collection of integers, known far and wide as "numbers." These numbers held a curious secret. For certain pairs of them, it was possible to rearrange or even leave the digits untouched to match one another, creating a unique bond. Your quest is to discover how many such distinct pairs exist within this enchanted array. Here's how the magic works: You must find pairs of numbers, where each pair consists of two indexes, i and j, such that i comes before j (i.e., 0 ≤ i < j < numbers.length). The twist? The number at position j can be transformed into the number at position i by swapping at most two of its digits. But here's an important note: sometimes, no swapping is needed at all! If the two numbers are already the same, they still count as a magical pair. Now, brave adventurer, your task is to complete the function named ziprecruiterCountDistinctSwappableDigitPairs, which will help you uncover the number of distinct pairs with these properties in the given array.

Function

ziprecruiterCountDistinctSwappableDigitPairs(nums: int[]) → int

Examples

Example 1

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

The valid pairs are 23 with 32, 156 with 651, and 156 with 165. Each later value becomes the earlier value after swapping at most two digit positions.

Example 2

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

Both pairs containing 321 are valid after swapping its first and last digits. The two equal values 123 also form a valid pair without a swap.

Constraints

Unknown for now

More ZipRecruiter problems

drafts saved locally
public int ziprecruiterCountDistinctSwappableDigitPairs(int[] nums) {
// write your code here
}
nums[1, 23, 156, 1650, 651, 165, 32]
expected3
checking account