Title: #767 - Reorganize String - LeetCode JavaScript Solutions
Open Graph Title: #767 - Reorganize String - LeetCode JavaScript Solutions
X Title: #767 - Reorganize String - LeetCode JavaScript Solutions
Description: 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...
Open Graph Description: 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...
X Description: 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...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/reorganize-string
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/reorganize-string","name":"#767 - Reorganize String - LeetCode JavaScript Solutions","description":"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...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {string} s\n * @return {string}\n */\nvar reorganizeString = function(s) {\n const charCount = new Map();\n for (const char of s) {\n charCount.set(char, (charCount.get(char) || 0) + 1);\n }\n\n const maxHeap = [...charCount.entries()]\n .sort((a, b) => b[1] - a[1]);\n\n if (maxHeap[0][1] > Math.ceil(s.length / 2)) {\n return '';\n }\n\n const result = [];\n let index = 0;\n\n while (maxHeap.length) {\n const [char, count] = maxHeap.shift();\n for (let i = 0; i < count; i++) {\n result[index] = char;\n index += 2;\n if (index >= s.length) index = 1;\n }\n }\n\n return result.join('');\n};","keywords":"LeetCode 767, #767 - Reorganize String, Medium, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/reorganize-string |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/reorganize-string/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0767-reorganize-string.js |
| String | https://leetcodejavascript.com/tags/string |
| Hash Table | https://leetcodejavascript.com/tags/hash-table |
| Sorting | https://leetcodejavascript.com/tags/sorting |
| Greedy | https://leetcodejavascript.com/tags/greedy |
| Heap Priority Queue | https://leetcodejavascript.com/tags/heap-priority-queue |
| Counting | https://leetcodejavascript.com/tags/counting |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow