Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

138. Reorganize String

MediumDay 41 of 60Heap / Priority Queue

Given a string s of lowercase letters, rearrange its characters so that no two neighbouring characters are the same. Return any such arrangement, or an empty string "" if it is impossible.

Example 1
Input: s = "aab"
Output: "aba"
Example 2
Input: s = "aaab"
Output: ""
Three a's need at least two other characters between them, but there is only one.
Constraints
  • 1 <= s.length <= 500
  • s has only lowercase English letters
Hints
Frequently asked at: Amazon, Google, Meta
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min