You can solve this problem by using a max-heap (priority queue) to always try to place the most frequent character at each position, ensuring that no two adjacent characters are the same. Here’s how you can implement it: Here’s the complete solution: Explanation: Time Complexity: Example: