Skip to content

869. Reordered Power of 2 👍

  • Time:
  • Space:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
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;
  }
}
1
2
3
4
class Solution:
  def reorderedPowerOf2(self, N: int) -> bool:
    count = collections.Counter(str(N))
    return any(Counter(str(1 << i)) == count for i in range(30))