Problem · Hash Table
Count Distinct Pairs
Learn this problemProblem 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[]) → longExamples
Example 1
nums = [1, 23, 156, 4738, 321, 72992, 231, 651, 32]return = 3The 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
numsis 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
- Longest Subarray with Sum at Most KOA · Seen Jul 2026
- Count Prefix Matches in a Sorted ArrayONSITE INTERVIEW · Seen Jul 2026
- Decode StringONSITE INTERVIEW · Seen Jul 2026
- Phone Keypad Letter CombinationsONSITE INTERVIEW · Seen Jul 2026
- Split a Log Outside QuotesONSITE INTERVIEW · Seen Jul 2026
- Ad Score Scheduler With DelayONSITE INTERVIEW · Seen Jul 2026
- Alternating-Color Binary Tree RootsONSITE INTERVIEW · Seen Jul 2026
- Route Pattern MatcherONSITE INTERVIEW · Seen Jul 2026