Leetcode: 2442. Count Number of Distinct Integers After Reverse Operations

Problem Statement

Understand the problem

Given an array \(a\), find the number of distinct elements on \(b\) where \(b\) contains elements of \(a\) and for each \(x\) in \(a\), it also contains the number generated by reversing the digits of \(x\).

Devise a plan

Pattern: Small constraints could allow a brute-force solution, so generate \(b\) and return its size. Time complexity and space are \(O(n)\).

Carry out the plan

class Solution:
    def countDistinctIntegers(self, nums: List[int]) -> int:
        seen = set(nums)
        for n in nums:
            seen.add(int(str(n)[::-1]))
        return len(seen)