1814. Count Nice Pairs in an Array

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 1814

class Solution:
    def countNicePairs(self, nums: List[int]) -> int:
        def rev(x):
            y = 0
            while x:
                y = y * 10 + x % 10
                x //= 10
            return y

        res = 0
        mod = 10**9 + 7
        cnt = Counter()
        for x in nums:
            y = x - rev(x)
            res += cnt[y]
            cnt[y] += 1
        return res % mod