Input: nums =[1023,2310,2130,213]Output: 4Explanation:
The almost equal pairs of elements are:*1023 and 2310. By swapping the digits 1 and 2, and then the digits 0 and 3in1023, you get 2310.*1023 and 213. By swapping the digits 1 and 0, and then the digits 1 and 2in1023, you get 0213, which is213.*2310 and 213. By swapping the digits 2 and 0, and then the digits 3 and 2in2310, you get 0213, which is213.*2310 and 2130. By swapping the digits 3 and 1in2310, you get 2130.
Input: nums =[1,10,100]Output: 3Explanation:
The almost equal pairs of elements are:*1 and 10. By swapping the digits 1 and 0in10, you get 01 which is1.*1 and 100. By swapping the second 0with the digit 1in100, you get 001, which is1.*10 and 100. By swapping the first 0with the digit 1in100, you get 010, which is10.
Since we can swap any two digits up to two times in either number, two numbers are almost equal if their digits can be rearranged to match with at most two swaps. For each number, we can generate all numbers reachable by up to two swaps and count how many times each appears in the array. For each pair, if their swapped forms match, they are almost equal.
classSolution {
public:int countAlmostEqualPairs(vector<int>& nums) {
unordered_map<int, int> freq;
for (int x : nums) freq[x]++;
int ans =0;
for (int i =0; i < nums.size(); ++i) {
string s = to_string(nums[i]);
set<int> seen;
int n = s.size();
for (int a =0; a < n; ++a) {
for (int b = a; b < n; ++b) {
swap(s[a], s[b]);
for (int c =0; c < n; ++c) {
for (int d = c; d < n; ++d) {
swap(s[c], s[d]);
int v = stoi(s);
if (!seen.count(v)) {
ans += freq[v] - (v == nums[i]);
seen.insert(v);
}
swap(s[c], s[d]);
}
}
swap(s[a], s[b]);
}
}
}
return ans /2;
}
};
classSolution {
funcountAlmostEqualPairs(nums: IntArray): Int {
val freq = mutableMapOf<Int, Int>()
for (x in nums) freq[x] = freq.getOrDefault(x, 0) + 1var ans = 0for (i in nums.indices) {
val s = nums[i].toString().toCharArray()
val seen = mutableSetOf<Int>()
val n = s.size
for (a in0 until n) {
for (b in a until n) {
s[a] = s[b].also { s[b] = s[a] }
for (c in0 until n) {
for (d in c until n) {
s[c] = s[d].also { s[d] = s[c] }
val v = String(s).toInt()
if (v !in seen) {
ans += freq.getOrDefault(v, 0) - if (v == nums[i]) 1else0 seen.add(v)
}
s[c] = s[d].also { s[d] = s[c] }
}
}
s[a] = s[b].also { s[b] = s[a] }
}
}
}
return ans / 2 }
}
classSolution:
defcountAlmostEqualPairs(self, nums: list[int]) -> int:
from collections import Counter
freq = Counter(nums)
ans =0for i, x in enumerate(nums):
s = list(str(x))
seen = set()
n = len(s)
for a in range(n):
for b in range(a, n):
s[a], s[b] = s[b], s[a]
for c in range(n):
for d in range(c, n):
s[c], s[d] = s[d], s[c]
v = int(''.join(s))
if v notin seen:
ans += freq[v] - (v == x)
seen.add(v)
s[c], s[d] = s[d], s[c]
s[a], s[b] = s[b], s[a]
return ans //2
impl Solution {
pubfncount_almost_equal_pairs(nums: Vec<i32>) -> i32 {
use std::collections::{HashMap, HashSet};
letmut freq = HashMap::new();
for&x in&nums {
*freq.entry(x).or_insert(0) +=1;
}
letmut ans =0;
for&x in&nums {
let s: Vec<char>= x.to_string().chars().collect();
let n = s.len();
letmut seen = HashSet::new();
for a in0..n {
for b in a..n {
letmut t = s.clone();
t.swap(a, b);
for c in0..n {
for d in c..n {
letmut u = t.clone();
u.swap(c, d);
let v: i32= u.iter().collect::<String>().parse().unwrap();
if!seen.contains(&v) {
ans += freq.get(&v).unwrap_or(&0) -if v == x { 1 } else { 0 };
seen.insert(v);
}
}
}
}
}
}
ans /2 }
}
⏰ Time complexity: O(n * d^4), where n is the length of nums and d is the maximum number of digits in any number. For each number, we try all pairs of swaps (O(d^4)).
🧺 Space complexity: O(n), for the frequency map and sets.