Skip to content

Commit 1044587

Browse files
authored
Create 1758-minimum-changes-to-make-alternating-binary-string.js
1 parent 519dede commit 1044587

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const minOperations = function(s) {
6+
const arr = s.split('')
7+
return Math.min(helper(arr, 0, '0'), helper(arr, 0, '1'))
8+
9+
function helper(arr, idx, ch) {
10+
if(idx === arr.length) return 0
11+
if(arr[idx] !== ch) return 1 + helper(arr, idx + 1, ch === '0' ? '1' : '0')
12+
else return helper(arr, idx + 1, ch === '0' ? '1' : '0')
13+
}
14+
};

0 commit comments

Comments
 (0)