Skip to content

Commit e2ecdb5

Browse files
committed
[LEET] 1758 Minimum Changes To Make Alternating Binary String (Easy)
1 parent 2d0f005 commit e2ecdb5

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

이용훈/10주차/260305.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var minOperations = function(s) {
6+
let mismatchStartWith0 = 0;
7+
let mismatchStartWith1 = 0;
8+
9+
for (let i = 0; i < s.length; i++) {
10+
const expected0 = (i % 2 === 0) ? '0' : '1';
11+
const expected1 = (i % 2 === 0) ? '1' : '0';
12+
13+
if (s[i] !== expected0) mismatchStartWith0++;
14+
if (s[i] !== expected1) mismatchStartWith1++;
15+
}
16+
17+
return Math.min(mismatchStartWith0, mismatchStartWith1);
18+
};

0 commit comments

Comments
 (0)