class Solution:
    def isHappy(self, n: int) -> bool:
        seen = set()
        while n not in seen and n != 1:
            seen.add(n)
            temp = 0
            while n > 0:
                temp += (n % 10) ** 2
                n //= 10
            print(temp)
            n = temp
        
        return n == 1

Complexity

Time:
Space:

Notes

Getting each digit in a number takes time.

Can the number go up to infinity?
A: No. Numbers with 4 or more digits will eventually come down to 3 digits. E.g. will square-sum up to , will be and so on even when we have 13 9s, it’ll be just . So, the numbers will either cycle or come down to 1.