Skip to main content
LeetCode 767, Medium. Topics: Hash Table, String, Greedy, Sorting, Heap (Priority Queue), Counting. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 14 parametrized pytest cases, and a playground notebook:

Problem

Given a string s, rearrange the characters of s so that any two adjacent characters are not the same. Return any possible rearrangement of s or return "" if not possible.

Examples

Constraints

  • 1 <= s.length <= 500
  • s consists of lowercase English letters.

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode 250, NeetCode All.
Last modified on August 25, 2026