class Solution {
public:
bool isHappy(int n) {
unordered_set<int> s;
while (true) {
if (n == 1) return true;
if (s.count(n)) return false;
s.insert(n);
string s = to_string(n);
n = 0;
for (int i = 0; i < s.size(); ++i)
n += (s[i] - '0') * (s[i] - '0');
}
}
};