Maximum Score After Splitting a String LeetCode solution in JavaScript

1
/**
2
* @param {string} s
3
* @return {number}
4
*/
5
var maxScore = function (s) {
6
const length = s.length;
7
let ones = 0;
8
let tmpScore = s[0] === "0" ? 1 : 0;
9
let score = tmpScore;
10

11
for (let i = 1; i < length - 1; i++) {
12
if (s[i] === "0") {
13
tmpScore += 1;
14
} else {
15
ones += 1;
16
tmpScore -= 1;
17
}
18

19
if (tmpScore > score) {
20
score = tmpScore;
21
}
22
}
23

24
ones += s[length - 1] === "1" ? 1 : 0;
25

26
return ones + score;
27
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0