public String rearrangeString(String str, int distance) {
        Map<Character, Integer> map = new HashMap<>();
        for (char c : str.toCharArray()) {
            if (map.containsKey(c)) {
                int count = map.get(c);
                map.put(c, ++count);
            } else {
                map.put(c, 1);
            }
        }
        PriorityQueue<StructHelper> maxHeap = new PriorityQueue<>(new Comparator<StructHelper>() {
            @Override
            public int compare(StructHelper o1, StructHelper o2) {
                if (o1.count > o2.count) {
                    return -1;
                } else if (o1.count < o2.count) {
                    return 1;
                } else {
                    return 0;
                }
            }
        });
        for (Map.Entry<Character, Integer> entry : map.entrySet()) {
            maxHeap.add(new StructHelper(entry.getKey(), entry.getValue()));
        }

        char[] res = new char[str.length()];
        while (!maxHeap.isEmpty()) {
            StructHelper tmp = maxHeap.remove();
            int j = 0;
            while (res[j] != 0) ++j;
            if (j + (tmp.count - 1) * distance >= res.length) return null;
            for (int i = 0; i < tmp.count; ++i) {
                res[j + i * distance] = tmp.c;
            }
        }

        return new String(res);
    }