class Solution: def reorderedPowerOf2(self, n: int) -> bool: powers_of_two = collections.defaultdict() powers_of_two[1] = collections.Counter("1") val = 1 while val <= 10 ** 9: val *= 2 powers_of_two[val] = collections.Counter(str(val)) c = collections.Counter(str(n)) for k in powers_of_two: print(powers_of_two[k]) if c == powers_of_two[k]: return True return False
there aren’t actually that many powers of two under 109, so we can just enumerate all of them.