Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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.
Example 1:
Input: s = "adcb"
Output: true
Explanation:
Split at index i = 1:
s[0..1] = "ad" with score = 1 + 4 = 5s[2..3] = "cb" with score = 3 + 2 = 5Both 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:
2 <= s.length <= 100s consists of lowercase English letters.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"adcb"
"bace"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3707: Equal Score Substrings
class Solution {
public boolean scoreBalance(String s) {
int n = s.length();
int l = 0, r = 0;
for (int i = 0; i < n; ++i) {
int x = s.charAt(i) - 'a' + 1;
r += x;
}
for (int i = 0; i < n - 1; ++i) {
int x = s.charAt(i) - 'a' + 1;
l += x;
r -= x;
if (l == r) {
return true;
}
}
return false;
}
}
// Accepted solution for LeetCode #3707: Equal Score Substrings
func scoreBalance(s string) bool {
var l, r int
for _, c := range s {
x := int(c-'a') + 1
r += x
}
for _, c := range s[:len(s)-1] {
x := int(c-'a') + 1
l += x
r -= x
if l == r {
return true
}
}
return false
}
# Accepted solution for LeetCode #3707: Equal Score Substrings
class Solution:
def scoreBalance(self, s: str) -> bool:
l = 0
r = sum(ord(c) - ord("a") + 1 for c in s)
for c in s[:-1]:
x = ord(c) - ord("a") + 1
l += x
r -= x
if l == r:
return True
return False
// Accepted solution for LeetCode #3707: Equal Score Substrings
fn score_balance(s: String) -> bool {
let mut right = s.bytes().fold(0, |acc, b| acc + (b - b'a' + 1) as i32);
let mut left = 0;
for b in s.bytes() {
let v = (b - b'a' + 1) as i32;
left += v as i32;
right -= v as i32;
dbg!(left, right);
if left == right {
return true;
}
}
false
}
fn main() {
let ret = score_balance(String::from("abdcd"));
println!("ret={ret}");
}
#[test]
fn test() {
assert!(score_balance("abdcd".to_string()));
assert!(score_balance("adcb".to_string()));
assert!(!score_balance("bace".to_string()));
}
// Accepted solution for LeetCode #3707: Equal Score Substrings
function scoreBalance(s: string): boolean {
let [l, r] = [0, 0];
for (const c of s) {
const x = c.charCodeAt(0) - 96;
r += x;
}
for (let i = 0; i < s.length - 1; ++i) {
const x = s[i].charCodeAt(0) - 96;
l += x;
r -= x;
if (l === r) {
return true;
}
}
return false;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.