LeetCode-in-Java

3707. Equal Score Substrings

Easy

You are given a string s consisting of lowercase English letters.

The score of a string is the sum of the positions of its characters in the alphabet, where 'a' = 1, 'b' = 2, …, 'z' = 26.

Determine whether there exists an index i such that the string can be split into two non-empty substrings s[0..i] and s[(i + 1)..(n - 1)] that have equal scores.

Return true if such a split exists, otherwise return false.

A substring is a contiguous non-empty sequence of characters within a string.

Example 1:

Input: s = “adcb”

Output: true

Explanation:

Split at index i = 1:

Both substrings have equal scores, so the output is true.

Example 2:

Input: s = “bace”

Output: false

Explanation:

No split produces equal scores, so the output is false.

Constraints:

Solution

public class Solution {
    public boolean scoreBalance(String s) {
        int total = 0;
        for (char c : s.toCharArray()) {
            total += c - 'a' + 1;
        }
        int prefix = 0;
        for (char c : s.toCharArray()) {
            prefix += c - 'a' + 1;
            if (2 * prefix == total) {
                return true;
            }
        }
        return false;
    }
}