Skip to content

Commit df11de7

Browse files
committed
Runtime: 94 ms (Top 77.32%) | Memory: 44.2 MB (Top 71.88%)
1 parent da5662a commit df11de7

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1+
// Runtime: 94 ms (Top 77.32%) | Memory: 44.2 MB (Top 71.88%)
12
var reorganizeString = function(s) {
23
const charMap = {};
34
const res = [];
4-
5+
56
// Store the count of each char
67
for (let char of s) {
78
charMap[char] = (charMap[char] || 0) + 1;
89
}
9-
10+
1011
// Sort in descending order by count
1112
const sortedMap = Object.entries(charMap).sort((a, b) => b[1] - a[1]);
12-
13+
1314
// Check if we can distribute the first char by every other position.
1415
// We only need to check the first char b/c the chars are ordered by count
1516
// so if the first char succeeds, all following chars will succeed
1617
if (sortedMap[0][1] > Math.floor((s.length + 1) / 2)) return '';
17-
18+
1819
let position = 0;
1920
for (let entry of sortedMap) {
2021
const char = entry[0]
@@ -25,12 +26,12 @@ var reorganizeString = function(s) {
2526
// for placing chars in odd positions
2627
res[position] = char;
2728
position +=2;
28-
29+
2930
// This will only happen once since total number of chars
3031
// will be exactly equal to the length of s
3132
if (position >= s.length) position = 1;
3233
}
3334
}
34-
35+
3536
return res.join('');
36-
};
37+
};

0 commit comments

Comments
 (0)