#3707
Easy Algorithms Equal score substrings
String Prefix Sum
56.7% acceptance
Feb 24, 2026
39
2
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn score_balance(s: String) -> bool {
let chars: Vec<i32> = s.bytes().map(|b| (b - b'a' + 1) as i32).collect();
let total: i32 = chars.iter().sum();
let mut prefix = 0i32;
for i in 0..chars.len() - 1 {
prefix += chars[i];
if prefix * 2 == total {
return true;
}
}
false
}
}