Skip to content

Latest commit

 

History

History
78 lines (58 loc) · 1.55 KB

_1422. Maximum Score After Splitting a String.md

File metadata and controls

78 lines (58 loc) · 1.55 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : January 01, 2025

Last updated : January 01, 2025


Related Topics : String, Prefix Sum

Acceptance Rate : 65.2 %


Solutions

C

int maxScore(char* s) {
    int left_zeros = 0, right_ones = 0;
    
    char* curr = s;
    while (*curr) {
        if (*curr == '1') {
            right_ones += 1;
        }
        curr += 1;
    }

    curr = s;
    int maxx = 0;

    while (*curr && *(curr + 1)) {
        if (*curr == '1') {
            right_ones -= 1;
        } else {
            left_zeros += 1;
        }
        
        if (left_zeros + right_ones > maxx) {
            maxx = left_zeros + right_ones;
        }
        curr += 1;
    }

    return maxx;
}

Python

class Solution:
    def maxScore(self, s: str) -> int:
        left_zeros, right_ones = 0, s.count('1')
        maxx = 0

        for c in s[:-1] :
            if c == '0' :
                left_zeros += 1
            else :
                right_ones -= 1
            maxx = max(maxx, left_zeros + right_ones)
        
        return maxx