aboutsummaryrefslogtreecommitdiff
path: root/max_score_split_str.py
blob: 5152d8a30a3897ac8643f59f90a321cc0ebc6bb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 1422. Maximum Score After Splitting a String

"""
given a string 's' of zeros and ones, return the maximum score after
splitting the string into two non-empty substrings (ie. left and right). the
score after splitting a string is the number of zeros in the left substring
plus the number of ones in the right substring.
"""


class Solution(object):
    def maxScore(self, s):
        """
        :type s: str
        :rtype: int
        """
        max_score = prefix_zero = 0
        suffix_one = s.count("1")
        for i in range(len(s) - 1):
            prefix_zero += s[i] == "0"
            suffix_one -= s[i] == "1"
            max_score = max(max_score, prefix_zero + suffix_one)

        return max_score


if __name__ == "__main__":
    obj = Solution()
    print(obj.maxScore("011101"))
    print(obj.maxScore("00111"))
    print(obj.maxScore("1111"))