Maximum Score After Splitting a String LeetCode solution in JavaScript
1/**2* @param {string} s3* @return {number}4*/5var maxScore = function (s) {6const length = s.length;7let ones = 0;8let tmpScore = s[0] === "0" ? 1 : 0;9let score = tmpScore;1011for (let i = 1; i < length - 1; i++) {12if (s[i] === "0") {13tmpScore += 1;14} else {15ones += 1;16tmpScore -= 1;17}1819if (tmpScore > score) {20score = tmpScore;21}22}2324ones += s[length - 1] === "1" ? 1 : 0;2526return ones + score;27};