[
counter
]
BinarySearch 0829 Reverse Equivalent Pairs
Problem statement
https://binarysearch.com/problems/Reverse-Equivalent-Pairs/
Solution
We need to find number of paris where x - f(x) = y - f(y)
. So, we can precalculate all of values and then use counter.
Complexity
It is O(n)
for time and space.
Code
class Solution:
def solve(self, nums):
A = [x - int(str(x)[::-1]) for x in nums]
cnt = Counter(A)
ans = 2 * len(A)
for x, f in cnt.items():
ans += f*(f-1)
return (ans//2) % (10**9 + 7)