All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0801_0900.s0869_reordered_power_of_2.Solution Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
package g0801_0900.s0869_reordered_power_of_2;

// #Medium #Math #Sorting #Counting #Enumeration

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class Solution {
    public boolean reorderedPowerOf2(int n) {
        int i = 0;
        while (Math.pow(2, i) < (long) n * 10) {
            if (isValid(String.valueOf((int) (Math.pow(2, i++))), String.valueOf(n))) {
                return true;
            }
        }
        return false;
    }

    private boolean isValid(String a, String b) {
        Map m = new HashMap<>();
        Map mTwo = new HashMap<>();
        for (char c : a.toCharArray()) {
            m.put(c, m.containsKey(c) ? m.get(c) + 1 : 1);
        }
        for (char c : b.toCharArray()) {
            mTwo.put(c, mTwo.containsKey(c) ? mTwo.get(c) + 1 : 1);
        }
        for (Map.Entry entry : mTwo.entrySet()) {
            if (!m.containsKey(entry.getKey())
                    || !Objects.equals(entry.getValue(), m.get(entry.getKey()))) {
                return false;
            }
        }
        return a.charAt(0) != '0' && m.size() == mTwo.size();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy