From da67dd676072a45b57086273e18c3ccda912fb80 Mon Sep 17 00:00:00 2001 From: aadil42 <77232799+aadil42@users.noreply.github.com> Date: Wed, 1 Jan 2025 09:03:32 +0530 Subject: [PATCH] Create 1422-maximum-score-after-splitting-a-string.js Solved maximum-score-after-splitting-a-string --- ...-maximum-score-after-splitting-a-string.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 javascript/1422-maximum-score-after-splitting-a-string.js diff --git a/javascript/1422-maximum-score-after-splitting-a-string.js b/javascript/1422-maximum-score-after-splitting-a-string.js new file mode 100644 index 000000000..363f42a6f --- /dev/null +++ b/javascript/1422-maximum-score-after-splitting-a-string.js @@ -0,0 +1,21 @@ +/** + * Array | Counting + * Time O(n) | Space O(n) (because we're making array when counting ones' freq) + * https://leetcode.com/problems/maximum-score-after-splitting-a-string + * @param {string} s + * @return {number} + */ +var maxScore = function(s) { + + let zeroCount = 0; + let oneCount = s.split("").filter((c) => c === "1").length; + + let maxSum = 0; + for (let i = 0; i < s.length - 1; i++) { + if (s[i] === "0") zeroCount++;; + if (s[i] === "1") oneCount--; + maxSum = Math.max(maxSum, zeroCount+oneCount); + } + + return maxSum; +};