Title: #2182 - Construct String With Repeat Limit - LeetCode JavaScript Solutions
Open Graph Title: #2182 - Construct String With Repeat Limit - LeetCode JavaScript Solutions
X Title: #2182 - Construct String With Repeat Limit - LeetCode JavaScript Solutions
Description: You are given a string s and an integer repeatLimit. Construct a new string repeatLimitedString using the characters of s such that no lette...
Open Graph Description: You are given a string s and an integer repeatLimit. Construct a new string repeatLimitedString using the characters of s such that no lette...
X Description: You are given a string s and an integer repeatLimit. Construct a new string repeatLimitedString using the characters of s such that no lette...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/construct-string-with-repeat-limit
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/construct-string-with-repeat-limit","name":"#2182 - Construct String With Repeat Limit - LeetCode JavaScript Solutions","description":"You are given a string s and an integer repeatLimit. Construct a new string repeatLimitedString using the characters of s such that no lette...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {string} s\n * @param {number} repeatLimit\n * @return {string}\n */\nvar repeatLimitedString = function(s, repeatLimit) {\n const frequency = new Array(26).fill(0);\n for (const char of s) {\n frequency[char.charCodeAt(0) - 97]++;\n }\n\n const result = [];\n let currentChar = 25;\n\n while (currentChar >= 0) {\n if (frequency[currentChar] === 0) {\n currentChar--;\n continue;\n }\n\n const useCount = Math.min(frequency[currentChar], repeatLimit);\n for (let i = 0; i < useCount; i++) {\n result.push(String.fromCharCode(currentChar + 97));\n }\n frequency[currentChar] -= useCount;\n\n if (frequency[currentChar] > 0) {\n let nextChar = currentChar - 1;\n while (nextChar >= 0 && frequency[nextChar] === 0) {\n nextChar--;\n }\n\n if (nextChar < 0) break;\n\n result.push(String.fromCharCode(nextChar + 97));\n frequency[nextChar]--;\n } else {\n currentChar--;\n }\n }\n\n return result.join('');\n};","keywords":"LeetCode 2182, #2182 - Construct String With Repeat Limit, 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/construct-string-with-repeat-limit |
| 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/construct-string-with-repeat-limit/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/2182-construct-string-with-repeat-limit.js |
| String | https://leetcodejavascript.com/tags/string |
| Hash Table | https://leetcodejavascript.com/tags/hash-table |
| 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