LeetCode Problem Workspace
Score of a String
Calculate the sum of absolute ASCII differences between adjacent characters to score any given string efficiently.
1
Topics
8
Code langs
3
Related
Practice Focus
Easy · String-driven solution strategy
Answer-first summary
Calculate the sum of absolute ASCII differences between adjacent characters to score any given string efficiently.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for String-driven solution strategy
This problem requires calculating a string's score by summing the absolute differences of adjacent character ASCII values. Start iterating through the string from the first character, compute the absolute difference with the next character, and accumulate the total. Using a direct string-driven approach ensures an O(n) runtime with minimal memory usage.
Problem Statement
Given a string s consisting only of lowercase English letters, define the score of s as the sum of absolute differences between the ASCII values of each pair of adjacent characters. For example, the difference between characters 'a' and 'c' contributes |97 - 99| = 2 to the total score.
Return the total score of the string s. For example, if s = "hello", calculate the score by summing |'h' - 'e'| + |'e' - 'l'| + |'l' - 'l'| + |'l' - 'o'|, resulting in 13. Ensure your solution handles strings up to length 100 efficiently.
Examples
Example 1
Input: s = "hello"
Output: 13
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
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 <= 100
- s consists only of lowercase English letters.
Solution Approach
Iterate and Compute Differences
Loop through the string from the first to the second-to-last character, calculate the absolute difference between each character and the next, and sum these values to get the total score. This directly follows the string-driven solution strategy.
Optimize for Constant Space
Keep a running total instead of storing differences in an array. Since only the previous character is needed for comparison, this maintains O(1) space complexity while still iterating over the string once.
Handle Edge Cases
Ensure the string has at least two characters as per constraints. The approach naturally handles repeated characters and alternating patterns without special conditions, following the typical failure mode of missing adjacent comparisons.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | O(n) |
| Space | O(1) |
Time complexity is O(n) because each character is visited once to compute its difference with the next. Space complexity is O(1) since only a running total and the previous character need storage.
What Interviewers Usually Probe
- Check if candidates correctly use ASCII values for adjacent characters.
- Watch for unnecessary data structures that increase space usage.
- Listen for clear explanation of the string-driven iterative pattern.
Common Pitfalls or Variants
Common pitfalls
- Forgetting to take the absolute value of differences.
- Attempting to store all differences instead of a running sum.
- Failing on repeated characters where difference is zero.
Follow-up variants
- Compute the score using the difference between Unicode code points for extended character sets.
- Return a cumulative array of scores at each position instead of a total sum.
- Apply the same scoring method to a list of strings and return their maximum score.
FAQ
What does the 'Score of a String' problem measure?
It measures the sum of absolute differences between ASCII values of adjacent characters in the string.
Can this solution handle very long strings?
Yes, the approach is O(n) in time and O(1) in space, efficiently handling strings up to length 100.
Do I need to store differences in an array?
No, you can maintain a running total of differences, keeping memory usage constant.
How do repeated characters affect the score?
Repeated characters contribute zero to the score since their ASCII difference is zero.
What is the key pattern for solving this problem?
The key pattern is a string-driven solution strategy where you iterate through characters and sum absolute differences efficiently.
Solution
Solution 1: Simulation
We directly traverse the string $s$, calculating the sum of the absolute differences of the ASCII codes of adjacent characters.
class Solution:
def scoreOfString(self, s: str) -> int:
return sum(abs(a - b) for a, b in pairwise(map(ord, s)))Continue Topic
string
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
String-driven solution strategy
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward