Reorganize String.md
December 10, 2025 ยท View on GitHub
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.
var reorganizeString = function(s) {
const map = new Map();
// 1. building map
s.split('').forEach((letter)=>{
map.set(letter,map.get(letter)+1||1);
})
// 1. sorting the map based on the occurances in descending order.
const sortedMap = new Map([...map.entries()].sort((a,b)=>b[1]-a[1]));
// 2. getting the first value of sorted map and checking if greater than half of string length
if(sortedMap.values().next().value > (s.length+1)/2) return "";
const res=[];
let index=0;
for(let [key,value] of sortedMap){
while(value--){
// 5. if it reaches the end of string, start filling from odd position.
if(index>=s.length) index=1;
// 4. adding elements at even position.
res[index]=key
index+=2
}
}
// converting array back to string
return res.join('');
};