Leetcode 1537 Solution

This article provides solution to leetcode question 1537 (maximum-score-after-splitting-a-string)

https://leetcode.com/problems/maximum-score-after-splitting-a-string

Solution

class Solution:
    def getScore(self, s, v):
        scores = []
        for i, ch in enumerate(s):
            score = 0 if i == 0 else scores[i - 1]
            if ch == v:
                score += 1
            scores.append(score)
        return scores

    def maxScore(self, s: str) -> int:
        scores1 = self.getScore(s, '0')
        scores2 = list(reversed(self.getScore(list(reversed(s)), '1')))

        ans = 0
        for i in range(len(s) - 1):
            ans = max(ans, scores1[i] + scores2[i + 1])
        return ans