↓Skip to main content
  1. LeetCode/

LeetCode 3707: Equal Score Substrings

·2 mins· ·
LeetCode Easy Prefix-Sum String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2025-10-11

  • Submissions: Wrong Answer → Accepted

Approach
#

Compute the total score, then accumulate the left score from the left; the right side is total - left (Prefix Sum Template 2). Both sides must be non-empty, so stop before the last character.

My Attempts
#

Attempt 1 used ord(char) as the score. ord is the alphabet position plus 96, so sides of different lengths gain a different 96 × length. For "bac", "ba" scores 2 + 1 = 3 and "c" scores 3, so the answer is True; with ord it is 195 versus 99, returning False. Using ord(char) - ord("a") + 1 passed.

Solution
#

class Solution:
    def scoreBalance(self, s: str) -> bool:
        scores = [ord(c) - ord("a") + 1 for c in s]
        total, left = sum(scores), 0
        for x in scores[:-1]:
            left += x
            if left * 2 == total:
                return True
        return False

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • The score is the alphabet position 1 to 26: subtract ord("a") and add 1.
  • The right side cannot be empty, so the last character is not a split point.

Interview Explanation
#

I compute the whole string’s score, then accumulate from the left; when the left is exactly half the total, a split works. Scores are alphabet positions, ord(c) - ord('a') + 1. Time O(n).

Related

LeetCode 3606: Coupon Code Validator
·2 mins
LeetCode Easy Hash-Table String Sorting
LeetCode 3606 note: filter valid coupons, then sort by (category order, code); character ranges must include z, Z and 9
LeetCode 1768: Merge Strings Alternately
·2 mins
LeetCode Easy Two-Pointers String
LeetCode 1768 note: take characters in turn with one index, then append whatever remains of the longer string
LeetCode 3136: Valid Word
·2 mins
LeetCode Easy String
LeetCode 3136 note: length at least 3, alphanumeric only, at least one vowel and at least one consonant; digits are not consonants
LeetCode 1189: Maximum Number of Balloons
·2 mins
LeetCode Easy Hash-Table String Counting
LeetCode 1189 note: each letter supports count // needed balloons, with l and o needed twice; take the minimum
LeetCode 1078: Occurrences After Bigram
·1 min
LeetCode Easy String
LeetCode 1078 note: split into words and check every three consecutive words, collecting the third when the first two match
LeetCode 1047: Remove All Adjacent Duplicates In String
·1 min
LeetCode Easy String Stack
LeetCode 1047 note: if the stack top equals the current character both vanish, otherwise push it; after a removal the new top may cancel with the next character