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. The score of a string is defined as the sum of the absolute difference between the ASCII values of adjacent characters.
Return the score of s.
Example 1:
Input: s = "hello"
Output: 13
Explanation:
The ASCII values of the characters in s are: 'h' = 104, 'e' = 101, 'l' = 108, 'o' = 111. So, the score of s would be |104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| = 3 + 7 + 0 + 3 = 13.
Example 2:
Input: s = "zaz"
Output: 50
Explanation:
The ASCII values of the characters in s are: 'z' = 122, 'a' = 97. So, the score of s would be |122 - 97| + |97 - 122| = 25 + 25 = 50.
Constraints:
2 <= s.length <= 100s consists only of lowercase English letters.Problem summary: You are given a string s. The score of a string is defined as the sum of the absolute difference between the ASCII values of adjacent characters. Return the score of s.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"hello"
"zaz"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3110: Score of a String
class Solution {
public int scoreOfString(String s) {
int ans = 0;
for (int i = 1; i < s.length(); ++i) {
ans += Math.abs(s.charAt(i - 1) - s.charAt(i));
}
return ans;
}
}
// Accepted solution for LeetCode #3110: Score of a String
func scoreOfString(s string) (ans int) {
for i := 1; i < len(s); i++ {
ans += abs(int(s[i-1]) - int(s[i]))
}
return
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #3110: Score of a String
class Solution:
def scoreOfString(self, s: str) -> int:
return sum(abs(a - b) for a, b in pairwise(map(ord, s)))
// Accepted solution for LeetCode #3110: Score of a String
impl Solution {
pub fn score_of_string(s: String) -> i32 {
s.as_bytes()
.windows(2)
.map(|w| (w[0] as i32 - w[1] as i32).abs())
.sum()
}
}
// Accepted solution for LeetCode #3110: Score of a String
function scoreOfString(s: string): number {
let ans = 0;
for (let i = 1; i < s.length; ++i) {
ans += Math.abs(s.charCodeAt(i) - s.charCodeAt(i - 1));
}
return ans;
}
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.