You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# https://leetcode.com/problems/count-almost-equal-pairs-ii/classSolution:
def countPairs(self, nums: List[int]) -> int:
d = len(str(max(nums)))
cnt = Counter(nums)
ret = sum(v * (v - 1) // 2 for v in cnt.values())
transform = defaultdict(list)
for x in cnt:
transform[x].append(x)
s = list(str(x).zfill(d))
for i in range(d):
for j in range(i):
if s[i] == s[j]:
continue
s[i], s[j] = s[j], s[i]
y = int("".join(s))
transform[y].append(x)
s[i], s[j] = s[j], s[i]
pairs = set()
for y, xvec in transform.items():
for x1, x2 in combinations(xvec, 2):
pairs.add((min(x1, x2), max(x1, x2)))
for x1, x2 in pairs:
ret += cnt[x1] * cnt[x2]
return ret