LeetCode Solutions

869. Reordered Power of 2

Time:

Space:

			

class Solution {
 public:
  bool reorderedPowerOf2(int N) {
    int count = counter(N);

    for (int i = 0; i < 30; ++i)
      if (counter(1 << i) == count)
        return true;

    return false;
  }

 private:
  int counter(int n) {
    int count = 0;

    for (; n > 0; n /= 10)
      count += pow(10, n % 10);

    return count;
  }
};
			

class Solution {
  public boolean reorderedPowerOf2(int N) {
    int count = counter(N);

    for (int i = 0; i < 30; ++i)
      if (counter(1 << i) == count)
        return true;

    return false;
  }

  private int counter(int n) {
    int count = 0;

    for (; n > 0; n /= 10)
      count += Math.pow(10, n % 10);

    return count;
  }
}
			

class Solution:
  def reorderedPowerOf2(self, N: int) -> bool:
    count = Counter(str(N))
    return any(Counter(str(1 << i)) == count for i in range(30))