Skip to content

Commit a4d144f

Browse files
authored
Create reordered-power-of-2.py
1 parent 82c3033 commit a4d144f

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Python/reordered-power-of-2.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Time: O((logn)^2)
2+
# Space: O(logn)
3+
4+
# Starting with a positive integer N,
5+
# we reorder the digits in any order (including the original order)
6+
# such that the leading digit is not zero.
7+
#
8+
# Return true if and only if we can do this in a way
9+
# such that the resulting number is a power of 2.
10+
#
11+
# Example 1:
12+
#
13+
# Input: 1
14+
# Output: true
15+
# Example 2:
16+
#
17+
# Input: 10
18+
# Output: false
19+
# Example 3:
20+
#
21+
# Input: 16
22+
# Output: true
23+
# Example 4:
24+
#
25+
# Input: 24
26+
# Output: false
27+
# Example 5:
28+
#
29+
# Input: 46
30+
# Output: true
31+
#
32+
# Note:
33+
# - 1 <= N <= 10^9
34+
35+
class Solution(object):
36+
def reorderedPowerOf2(self, N):
37+
"""
38+
:type N: int
39+
:rtype: bool
40+
"""
41+
count = collections.Counter(str(N))
42+
return any(count == collections.Counter(str(1 << i))
43+
for i in xrange(31))

0 commit comments

Comments
 (0)