The same idea c++ solution would timeout

class Solution {
public:
bool judgeSquareSum(int c) {
int base=0;
int square=0;
unordered_set<int> squares;
while (square<=c){
if (square==c)
return true;
// 2=1^2+1^2 should return true, so insert first
squares.insert(square);
auto it=squares.find(c-square);
if (it!=squares.end())
return true;
base++;
square=base*base;
}
return false;
}
};