Reorganize String
Try to solve the Reorganize String problem.
Statement
Given a string, str
, rearrange it so that any two adjacent characters are not the same. If such a reorganization of the characters is possible, output any possible valid arrangement. Otherwise, return an empty string.
Constraints:
-
str.length
- Input string consists of lowercase English letters.
Examples
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check that if you’re solving the correct problem:
Reorganize String
(Select all that apply.) What is the output if str = "programming"
? Multi-select
“programing”
“rgmrgmpiano”
“rmpimrggano”
“programimng”
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Try it yourself
Implement your solution in the following coding playground:
import { MaxHeap, MinHeap } from "./Heap.js";/* The following definition is for MinHeap.You can use the same methods for MaxHeap.class MinHeap {size(); // return number of elementspeek(); // return top element without removingpush(val); // insert elementpop(); // remove and return top element}*/export function reorganizeString(str){// Replace this placeholder return statement with your codereturn ""}