⚡Flashcard Drill➔
NeetCode #75LC-1422EasyArrays & Hashing
← Back to All Problems

#75 · #1422 · Maximum Score After Splitting a String(分割字符串的最大得分)

📌 Problem Statement & Constraints

Given a string s of 0s and 1s, split it into two non-empty parts. The score is the number of 0s in the left part plus the number of 1s in the right part. Return the maximum score. Constraints: 2 <= s.length <= 500.

💡 Core Algorithmic Approaches

  1. Precompute the total number of ones so the right part's one-count is available in O(1).
  2. Sweep the split point from left to right, maintaining left_ones and deriving left_zeros = (i + 1) - left_ones.
  3. The right part's ones are total_ones - left_ones, so the score is left_zeros + (total_ones - left_ones).
  4. The split point only runs up to n - 2, since the right part must be non-empty.

💻 Benchmark Python3 Implementation

class Solution:
    def maxScore(self, s: str) -> int:
        total_ones = s.count("1")
        left_ones = 0
        best = 0
        for i in range(len(s) - 1):        # right part must be non-empty
            if s[i] == "1":
                left_ones += 1
            left_zeros = (i + 1) - left_ones
            best = max(best, left_zeros + (total_ones - left_ones))
        return best

⚡ Complexity Deep Dive

⏱️ Time Complexity
O(n): one pass to count the ones and one pass to evaluate the splits.
💾 Space Complexity
O(1): two counters.

⚠️ Interview Pitfalls & Follow-ups

  • Splitting at the last index: the right part must be non-empty, so the loop stops at n - 2.
  • Recounting the right part's ones each time: O(n^2). Deriving it from the total makes each step O(1).
  • Counting the left part's zeros by scanning: use the identity zeros = length - ones instead.
  • Initialising best to a negative number: the score is always non-negative, and 0 is a valid score for s = "11"... actually the score for "11" is 0 + 1 = 1, so initialising to 0 is safe because at least one split is always evaluated.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔

🧩 Related Arrays & Hashing Problems

中文速览与面试要点

分割字符串的最大得分

给你一个由若干 0 和 1 组成的字符串 s,请你计算并返回将该字符串分割成两个 非空 子字符串(即左子字符串和右子字符串)所能获得的最大得分。「分割字符串的得分」为左子字符串中 0 的数量加上右子字符串中 1 的数量。约束:2 ≤ s.length ≤ 500;s 仅由字符 0 和 1 组成。

核心要点

  • 一次遍历 + 增量维护(标准解) :先统计整个字符串中 1 的总数 ones 。从左往右扫描,维护 zeros (当前左半的 0 个数)。
  • 切分点 i 的得分 :扫描到位置 i(作为左半的最后一个字符)时,左半 0 的个数为 zeros,右半 1 的个数为 ones(因为每当左半吃掉一个 1,就把 ones 减一)。
  • 更新规则 :遇到字符 0 则 zeros += 1 ;遇到字符 1 则 ones -= 1 (它从左半划入,不再属于右半)。每步更新最优得分。
  • 切分点范围 :左半非空且右半非空,因此 i 从 0 到 n-2。

常见陷阱

  • 循环范围写成 range(len(s)) :i 取到 n-1 时右半为空,违反「两个非空子串」的约束。必须写 range(len(s) - 1) 。
  • 每个切分点都重新统计左右两半 :退化为 O(n²)。必须用增量维护。
  • 忘记遇到 1 时要 ones -= 1 :只增加 zeros 而不减少 ones,会导致右半的 1 被重复计入。
  • 初始化 best = 0 的合理性 :s 至少含 2 个字符,得分至少为 0,初值 0 安全。

参考实现

class Solution:
    def maxScore(self, s: str) -> int:
        ones = s.count('1')          # 右半初始为整个字符串
        zeros = 0                    # 左半初始为空
        best = 0
        for i in range(len(s) - 1):  # 左半非空、右半非空
            if s[i] == '0':
                zeros += 1
            else:
                ones -= 1
            best = max(best, zeros + ones)
        return best