LeetCode solutions

4052. Equal Score Substrings

My accepted Python solution to LeetCode problem 4052, Equal Score Substrings, running in 3ms.

  • Difficulty: Easy
  • Python
  • Runtime 3ms
  • Memory 17.3MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 3ms, memory 17.3MB, accepted 2025-12-30.

python
class Solution:
    def scoreBalance(self, s: str) -> bool:
        total = sum(ord(c) - ord('a') + 1 for c in s)
        if total % 2 != 0:
            return False
        target = total // 2
        prefix_sum = 0
        for i in range(len(s) - 1):  # -1 because both parts must be non-empty
            prefix_sum += ord(s[i]) - ord('a') + 1
            if prefix_sum == target:
                return True
        return False

Source