https://leetcode.com/problems/reordered-power-of-2/
Then I just compare
If
https://leetcode.com/problems/reordered-power-of-2/discuss/149825/JAVA-Naive-Backtracking-15-lines
Starting with a positive integer
N
, we reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return
true
if and only if we can do this in a way such that the resulting number is a power of 2.
https://leetcode.com/problems/reordered-power-of-2/discuss/149843/C%2B%2BJavaPython-Straight-Forward
counter
will counter the number of digits 9876543210 in the given number.Then I just compare
counter(N)
with all counter(power of 2)
.1 <= N <= 10^9
, so up to 8 same digits.If
N > 10^9
, we can use a hash map. public boolean reorderedPowerOf2(int N) {
long c = counter(N);
for (int i = 0; i < 32; i++)
if (counter(1 << i) == c) return true;
return false;
}
public long counter(int N) {
long res = 0;
for (; N > 0; N /= 10) res += (int)Math.pow(10, N % 10);
return res;
}
We can check whether two numbers have the same digits by comparing the count of their digits. For example, 338 and 833 have the same digits because they both have exactly two 3's and one 8.
Since could only be a power of 2 with 9 digits or less (namely, ), we can just check whether has the same digits as any of these possibilities.
- Time Complexity: . There are different candidate powers of 2, and each comparison has time complexity.
- Space Complexity: .
public boolean reorderedPowerOf2(int N) {
int[] A = count(N);
for (int i = 0; i < 31; ++i)
if (Arrays.equals(A, count(1 << i)))
return true;
return false;
}
// Returns the count of digits of N
// Eg. N = 112223334, returns [0,2,3,3,1,0,0,0,0,0]
public int[] count(int N) {
int[] ans = new int[10];
while (N > 0) {
ans[N % 10]++;
N /= 10;
}
return ans;
}
The idea here is similar to that of group Anagrams problem (Leetcode #49).
First, we convert the input number (N) into a string and sort the string. Next, we get the digits that form the power of 2 (by using 1 << i and vary i), convert them into a string, and then sort them. As we convert the powers of 2 (and there are only 31 that are <= 10^9), for each power of 2, we compare if the string is equal to that of string based on N. If the two strings are equal, then we return true.
public boolean reorderedPowerOf2(int N) {
char[] a1 = String.valueOf(N).toCharArray();
Arrays.sort(a1);
String s1 = new String(a1);
for (int i = 0; i < 31; i++) {
char[] a2 = String.valueOf((int)(1 << i)).toCharArray();
Arrays.sort(a2);
String s2 = new String(a2);
if (s1.equals(s2)) return true;
}
return false;
}
For each permutation of the digits of
N
, let's check if that permutation is a power of 2.
Algorithm
This approach has two steps: how will we generate the permutations of the digits, and how will we check that the permutation represents a power of 2?
To generate permutations of the digits, we place any digit into the first position (
start = 0
), then any of the remaining digits into the second position (start = 1
), and so on. In Python, we can use the builtin function itertools.permutations
.
To check whether a permutation represents a power of 2, we check that there is no leading zero, and divide out all factors of 2. If the result is
1
(that is, it contained no other factors besides 2
), then it was a power of 2. In Python, we can use the check bin(N).count('1') == 1
.- Time Complexity: . Note that is the number of digits in the binary representation of . For each of permutations of the digits of , we need to check that it is a power of 2 in time.
- Space Complexity: , the space used by
A
(orcand
in Python).
public boolean reorderedPowerOf2(int N) {
// Build eg. N = 128 -> A = [1, 2, 8]
String S = Integer.toString(N);
int[] A = new int[S.length()];
for (int i = 0; i < S.length(); ++i)
A[i] = S.charAt(i) - '0';
return permutations(A, 0);
}
// Return true if A represents a valid power of 2
public boolean isPowerOfTwo(int[] A) {
if (A[0] == 0)
return false; // no leading zero
// Build eg. A = [1, 2, 8] -> N = 128
int N = 0;
for (int x : A)
N = 10 * N + x;
// Remove the largest power of 2
while (N > 0 && ((N & 1) == 0))
N >>= 1;
// Check that there are no other factors besides 2
return N == 1;
}
/**
* Returns true if some permutation of (A[start], A[start+1], ...) can result in
* A representing a power of 2.
*/
public boolean permutations(int[] A, int start) {
if (start == A.length)
return isPowerOfTwo(A);
// Choose some index i from [start, A.length - 1]
// to be placed into position A[start].
for (int i = start; i < A.length; ++i) {
// Place A[start] with value A[i].
swap(A, start, i);
// For each such placement of A[start], if a permutation
// of (A[start+1], A[start+2], ...) can result in A
// representing a power of 2, return true.
if (permutations(A, start + 1))
return true;
// Restore the array to the state it was in before
// A[start] was placed with value A[i].
swap(A, start, i);
}
return false;
}
public void swap(int[] A, int i, int j) {
int t = A[i];
A[i] = A[j];
A[j] = t;
}
https://leetcode.com/problems/reordered-power-of-2/discuss/149825/JAVA-Naive-Backtracking-15-lines
public boolean reorderedPowerOf2(int N) {
char[] ca=(N+"").toCharArray();
return helper(ca, 0, new boolean[ca.length]);
}
public boolean helper(char[] ca, int cur, boolean[] used){
if (cur!=0 && (cur+"").length()==ca.length){
if (Integer.bitCount(cur)==1) return true;
return false;
}
for (int i=0; i<ca.length; i++){
if (used[i]) continue;
used[i]=true;
if (helper(ca, cur*10+ca[i]-'0', used)) return true;
used[i]=false;
}
return false;
}
It would be faster if you use memo to prune.
public boolean reorderedPowerOf2(int N) {
char[] ca=(N+"").toCharArray();
return helper(ca, 0, new boolean[ca.length], new HashSet<Integer>());
}
public boolean helper(char[] ca, int cur, boolean[] used, HashSet<Integer> vis){
if (!vis.add(cur)) return false;
if (cur!=0 && (cur+"").length()==ca.length){
if (Integer.bitCount(cur)==1) return true;
return false;
}
for (int i=0; i<ca.length; i++){
if (used[i]) continue;
used[i]=true;
if (helper(ca, cur*10+ca[i]-'0', used, vis)) return true;
used[i]=false;
}
return false;
}