Count Almost Equal Pairs I - Problem
You are given an array nums consisting of positive integers.
We call two integers x and y in this problem almost equal if both integers can become equal after performing the following operation at most once:
Choose either x or y and swap any two digits within the chosen number.
Return the number of indices i and j in nums where i < j such that nums[i] and nums[j] are almost equal.
Note: It is allowed for an integer to have leading zeros after performing an operation.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [3,12,30,17,21]
›
Output:
2
💡 Note:
Almost equal pairs: (12,21) because 12→21 by swapping digits, and (30,3) because 30→03→3 by swapping. Total count is 2.
Example 2 — No Swaps Needed
$
Input:
nums = [1,1,1,1,1]
›
Output:
10
💡 Note:
All numbers are already equal, so every pair (i,j) where i
Example 3 — No Almost Equal Pairs
$
Input:
nums = [123,456]
›
Output:
0
💡 Note:
No way to make 123 and 456 equal by swapping digits in either number.
Constraints
- 1 ≤ nums.length ≤ 100
- 1 ≤ nums[i] ≤ 106
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code