File tree 2 files changed +42
-1
lines changed
2 files changed +42
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,486 LeetCode solutions in JavaScript
1
+ # 1,487 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1311
1311
1697|[ Checking Existence of Edge Length Limited Paths] ( ./solutions/1697-checking-existence-of-edge-length-limited-paths.js ) |Hard|
1312
1312
1700|[ Number of Students Unable to Eat Lunch] ( ./solutions/1700-number-of-students-unable-to-eat-lunch.js ) |Easy|
1313
1313
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|
1314
1315
1716|[ Calculate Money in Leetcode Bank] ( ./solutions/1716-calculate-money-in-leetcode-bank.js ) |Easy|
1315
1316
1718|[ Construct the Lexicographically Largest Valid Sequence] ( ./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js ) |Medium|
1316
1317
1726|[ Tuple with Same Product] ( ./solutions/1726-tuple-with-same-product.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments