Question:
Given a string of lowercase characters, reorder them such that the same characters are at least distance d from each other.
Input: { a, b, b }, distance = 2
Output: { b, a, b }
public void reorder(int[] A, int d){ // Stats Mapmap = new HashMap<>(); for (int i : A) { Integer occurance = map.get(i); if (occurance == null) occurance = 0; occurance++; map.put(i, occurance); } // Sort List > list = new ArrayList<>(map.entrySet()); Collections.sort(list, new Comparator >() { public int compare(Entry a, Entry b) { return -1 * Integer.compare(a.getValue(), b.getValue()); } }); // Re-assign int curIndex = 0; Map.Entry cur = list.get(curIndex); int curOccurance = 0; for (int offset = 0; offset < d; offset++) { for (int i = offset; i < A.length; i += d) { A[i] = cur.getKey(); curOccurance++; if (curOccurance == cur.getValue()) { curIndex++; if (curIndex == list.size()) return; cur = list.get(curIndex); curOccurance = 0; } } }}