#3267

Count Almost Equal Pairs II

hard · 27% accepted · 81 likes · top 5%

array · hash table · sorting · counting · enumeration

⊣ practice⊣ open on leetcode ↗

Description

Attention: In this version, the number of operations that can be performed, has been increased to twice.

You are given an array nums consisting of positive integers.

We call two integers x and y almost equal if both integers can become equal after performing the following operation at most twice:

- 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 that it is allowed for an integer to have leading zeros after performing an operation.

Solution