1
var reorganizeString = function (s) {5
// Store the count of each char7
charMap[char] = (charMap[char] || 0) + 1;10
// Sort in descending order by count11
const sortedMap = Object.entries(charMap).sort((a, b) => b[1] - a[1]);13
// Check if we can distribute the first char by every other position.14
// We only need to check the first char b/c the chars are ordered by count15
// so if the first char succeeds, all following chars will succeed16
if (sortedMap[0][1] > Math.floor((s.length + 1) / 2)) return "";19
for (let entry of sortedMap) {20
const char = entry[0];21
const count = entry[1];22
for (let j = 0; j < count; j++) {23
// Distribute the current char every other position. The same char24
// will never be placed next to each other even on the 2nd loop25
// for placing chars in odd positions29
// This will only happen once since total number of chars30
// will be exactly equal to the length of s31
if (position >= s.length) position = 1;