358. Rearrange String k Distance Apart

;  |     Back to Homepage   |     Back to Code List


class Solution {
    public String rearrangeString(String s, int k) {
        if (k == 0 || s.length() <= 1) return s;
        int[] map = new int[26];
        for (char c : s.toCharArray()) {
            map[c - 'a']++;
        }
        
        PriorityQueue<int[]> heap = new PriorityQueue<>((a, b) -> 
        a[1] == b[1] ? a[0] - b[0] : b[1] - a[1]);
        for (int i = 0; i < 26; i++) {
            if (map[i] > 0) {
                heap.offer(new int[]{i, map[i]});
            }
        }
        
        StringBuilder sb = new StringBuilder();
        while (!heap.isEmpty()) {
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < k; i++) {
                int[] cur = heap.poll();
                sb.append((char) (cur[0] + 'a'));
                list.add(cur[0]);
                
                if (heap.size() == 0) {
                    if (i != k - 1 && sb.length() != s.length()) return "";
                    break;
                }
            }
            
            for (int i : list) {
                if (--map[i] > 0) {
                    heap.offer(new int[]{i, map[i]});
                }
            }
        }
        
        return sb.toString();
    }
}