Title: #1702 - Maximum Binary String After Change - LeetCode JavaScript Solutions
Open Graph Title: #1702 - Maximum Binary String After Change - LeetCode JavaScript Solutions
X Title: #1702 - Maximum Binary String After Change - LeetCode JavaScript Solutions
Description: You are given a binary string binary consisting of only 0's or 1's. You can apply each of the following operations any number of t...
Open Graph Description: You are given a binary string binary consisting of only 0's or 1's. You can apply each of the following operations any number of t...
X Description: You are given a binary string binary consisting of only 0's or 1's. You can apply each of the following operations any number of t...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/maximum-binary-string-after-change
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/maximum-binary-string-after-change","name":"#1702 - Maximum Binary String After Change - LeetCode JavaScript Solutions","description":"You are given a binary string binary consisting of only 0's or 1's. You can apply each of the following operations any number of t...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {string} binary\n * @return {string}\n */\nvar maximumBinaryString = function(binary) {\n const n = binary.length;\n let zeroCount = 0;\n let firstZero = -1;\n\n for (let i = 0; i < n; i++) {\n if (binary[i] === '0') {\n if (firstZero === -1) firstZero = i;\n zeroCount++;\n }\n }\n\n if (zeroCount <= 1) return binary;\n\n const result = new Array(n).fill('1');\n result[firstZero + zeroCount - 1] = '0';\n\n return result.join('');\n};","keywords":"LeetCode 1702, #1702 - Maximum Binary String After Change, 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/maximum-binary-string-after-change |
| 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/maximum-binary-string-after-change/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/1702-maximum-binary-string-after-change.js |
| String | https://leetcodejavascript.com/tags/string |
| Greedy | https://leetcodejavascript.com/tags/greedy |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow