#767

Reorganize String

medium · verified · 56.9% accepted · 9,193 likes · top 52%

hash table · string · greedy · sorting · heap (priority queue) · counting

⊣ practice⊣ open on leetcode ↗

Description

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.

Example 1:

Input: s = "aab"
Output: "aba"

Example 2:

Input: s = "aaab"
Output: ""

Solution