Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit efd66ff

Browse files
committed
Add solution #1758
1 parent dd9019a commit efd66ff

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-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,523 LeetCode solutions in JavaScript
1+
# 1,524 LeetCode solutions in JavaScript
22

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

@@ -1353,6 +1353,7 @@
13531353
1753|[Maximum Score From Removing Stones](./solutions/1753-maximum-score-from-removing-stones.js)|Medium|
13541354
1754|[Largest Merge Of Two Strings](./solutions/1754-largest-merge-of-two-strings.js)|Medium|
13551355
1755|[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|
13561357
1764|[Form Array by Concatenating Subarrays of Another Array](./solutions/1764-form-array-by-concatenating-subarrays-of-another-array.js)|Medium|
13571358
1765|[Map of Highest Peak](./solutions/1765-map-of-highest-peak.js)|Medium|
13581359
1768|[Merge Strings Alternately](./solutions/1768-merge-strings-alternately.js)|Easy|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
};

0 commit comments

Comments
 (0)