LeetCode 358: Rearrange String k Distance Apart Solution

Master LeetCode problem 358 (Rearrange String k Distance Apart), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

358. Rearrange String k Distance Apart

Problem Explanation

Explanation:

The problem asks us to rearrange a string such that no two same characters are K distance apart. If it is not possible to rearrange the string, return an empty string.

To solve this problem, we can use a greedy approach. We will start by constructing a frequency map of characters in the input string. Then, we will use a priority queue to keep track of the characters that need to be placed in the output string based on their frequencies. At each step, we will pop the top element from the priority queue, append it to the output string, and decrease its frequency. We will also keep track of the characters that are waiting to be placed back into the priority queue after K distance. Solution:

Solution Code

import java.util.*;

class Solution {
    public String rearrangeString(String s, int k) {
        if (k <= 1) return s;

        Map<Character, Integer> freqMap = new HashMap<>();
        for (char c : s.toCharArray()) {
            freqMap.put(c, freqMap.getOrDefault(c, 0) + 1);
        }

        PriorityQueue<Map.Entry<Character, Integer>> maxHeap = new PriorityQueue<>(
            (a, b) -> b.getValue() - a.getValue()
        );
        maxHeap.addAll(freqMap.entrySet());

        Queue<Map.Entry<Character, Integer>> waitQueue = new LinkedList<>();
        StringBuilder rearranged = new StringBuilder();

        while (!maxHeap.isEmpty()) {
            Map.Entry<Character, Integer> curr = maxHeap.poll();
            rearranged.append(curr.getKey());
            curr.setValue(curr.getValue() - 1);
            waitQueue.offer(curr);

            if (waitQueue.size() < k) continue;

            Map.Entry<Character, Integer> front = waitQueue.poll();
            if (front.getValue() > 0) {
                maxHeap.offer(front);
            }
        }

        return rearranged.length() == s.length() ? rearranged.toString() : "";
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 358 (Rearrange String k Distance Apart)?

This page provides optimized solutions for LeetCode problem 358 (Rearrange String k Distance Apart) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 358 (Rearrange String k Distance Apart)?

The time complexity for LeetCode 358 (Rearrange String k Distance Apart) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 358 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 358 in Java, C++, or Python.

Back to LeetCode Solutions