Skip to content

Commit 39913f2

Browse files
committed
Add solution #1702
1 parent c7c487f commit 39913f2

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,486 LeetCode solutions in JavaScript
1+
# 1,487 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1311,6 +1311,7 @@
13111311
1697|[Checking Existence of Edge Length Limited Paths](./solutions/1697-checking-existence-of-edge-length-limited-paths.js)|Hard|
13121312
1700|[Number of Students Unable to Eat Lunch](./solutions/1700-number-of-students-unable-to-eat-lunch.js)|Easy|
13131313
1701|[Average Waiting Time](./solutions/1701-average-waiting-time.js)|Medium|
1314+
1702|[Maximum Binary String After Change](./solutions/1702-maximum-binary-string-after-change.js)|Medium|
13141315
1716|[Calculate Money in Leetcode Bank](./solutions/1716-calculate-money-in-leetcode-bank.js)|Easy|
13151316
1718|[Construct the Lexicographically Largest Valid Sequence](./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js)|Medium|
13161317
1726|[Tuple with Same Product](./solutions/1726-tuple-with-same-product.js)|Medium|
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 1702. Maximum Binary String After Change
3+
* https://leetcode.com/problems/maximum-binary-string-after-change/
4+
* Difficulty: Medium
5+
*
6+
* You are given a binary string binary consisting of only 0's or 1's. You can apply each of
7+
* the following operations any number of times:
8+
* - Operation 1: If the number contains the substring "00", you can replace it with "10".
9+
* - For example, "00010" -> "10010"
10+
* - Operation 2: If the number contains the substring "10", you can replace it with "01".
11+
* - For example, "00010" -> "00001"
12+
*
13+
* Return the maximum binary string you can obtain after any number of operations. Binary
14+
* string x is greater than binary string y if x's decimal representation is greater than
15+
* y's decimal representation.
16+
*/
17+
18+
/**
19+
* @param {string} binary
20+
* @return {string}
21+
*/
22+
var maximumBinaryString = function(binary) {
23+
const n = binary.length;
24+
let zeroCount = 0;
25+
let firstZero = -1;
26+
27+
for (let i = 0; i < n; i++) {
28+
if (binary[i] === '0') {
29+
if (firstZero === -1) firstZero = i;
30+
zeroCount++;
31+
}
32+
}
33+
34+
if (zeroCount <= 1) return binary;
35+
36+
const result = new Array(n).fill('1');
37+
result[firstZero + zeroCount - 1] = '0';
38+
39+
return result.join('');
40+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy