File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed
Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change 1- # 1,523 LeetCode solutions in JavaScript
1+ # 1,524 LeetCode solutions in JavaScript
22
33[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
44
135313531753|[ Maximum Score From Removing Stones] ( ./solutions/1753-maximum-score-from-removing-stones.js ) |Medium|
135413541754|[ Largest Merge Of Two Strings] ( ./solutions/1754-largest-merge-of-two-strings.js ) |Medium|
135513551755|[ Closest Subsequence Sum] ( ./solutions/1755-closest-subsequence-sum.js ) |Hard|
1356+ 1758|[ Minimum Changes To Make Alternating Binary String] ( ./solutions/1758-minimum-changes-to-make-alternating-binary-string.js ) |Easy|
135613571764|[ Form Array by Concatenating Subarrays of Another Array] ( ./solutions/1764-form-array-by-concatenating-subarrays-of-another-array.js ) |Medium|
135713581765|[ Map of Highest Peak] ( ./solutions/1765-map-of-highest-peak.js ) |Medium|
135813591768|[ Merge Strings Alternately] ( ./solutions/1768-merge-strings-alternately.js ) |Easy|
Original file line number Diff line number Diff line change 1+ /**
2+ * 1758. Minimum Changes To Make Alternating Binary String
3+ * https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string/
4+ * Difficulty: Easy
5+ *
6+ * You are given a string s consisting only of the characters '0' and '1'. In one operation, you can
7+ * change any '0' to '1' or vice versa.
8+ *
9+ * The string is called alternating if no two adjacent characters are equal. For example, the string
10+ * "010" is alternating, while the string "0100" is not.
11+ *
12+ * Return the minimum number of operations needed to make s alternating.
13+ */
14+
15+ /**
16+ * @param {string } s
17+ * @return {number }
18+ */
19+ var minOperations = function ( s ) {
20+ let changesToZeroStart = 0 ;
21+ let changesToOneStart = 0 ;
22+
23+ for ( let i = 0 ; i < s . length ; i ++ ) {
24+ const expectedZeroStart = i % 2 === 0 ? '0' : '1' ;
25+ const expectedOneStart = i % 2 === 0 ? '1' : '0' ;
26+ if ( s [ i ] !== expectedZeroStart ) changesToZeroStart ++ ;
27+ if ( s [ i ] !== expectedOneStart ) changesToOneStart ++ ;
28+ }
29+
30+ return Math . min ( changesToZeroStart , changesToOneStart ) ;
31+ } ;
You can’t perform that action at this time.
0 commit comments