Back to blog
Apr 17, 2026
4 min read

Score of a String

Calculate the sum of absolute differences between ASCII values of adjacent characters in a string.

Difficulty: Easy | Acceptance: 91.30% | Paid: No Topics: String

You are given a string s. The score of s is the sum of the absolute difference between the ASCII values of adjacent characters.

Return the score of s.

Examples

Example 1:

Input: s = "hello"
Output: 13
Explanation:
The ASCII values of the characters in s are: 'h' = 104, 'e' = 101, 'l' = 108, 'l' = 108, 'o' = 111.
The score of s is |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, 'z' = 122.
The score of s is |122 - 97| + |97 - 122| = 25 + 25 = 50.

Constraints

2 <= s.length <= 100
s consists only of lowercase English letters.

Iterative Simulation

Intuition We can iterate through the string once, comparing each character with its neighbor to accumulate the total score.

Steps

  • Initialize a variable score to 0.
  • Loop through the string from the first character to the second-to-last character.
  • In each iteration, calculate the absolute difference between the ASCII value of the current character s[i] and the next character s[i+1].
  • Add this difference to score.
  • Return score.
python
class Solution:
    def scoreOfString(self, s: str) -&gt; int:
        score = 0
        for i in range(len(s) - 1):
            score += abs(ord(s[i]) - ord(s[i+1]))
        return score

Complexity

  • Time: O(n), where n is the length of the string. We traverse the string once.
  • Space: O(1), we only use a constant amount of extra space for the score variable.
  • Notes: This is the most efficient approach for this problem.

Functional Approach

Intuition We can utilize functional programming constructs like zip, map, and reduce (or streams) to calculate the differences and sum them up in a declarative way.

Steps

  • Create a sequence of pairs of adjacent characters.
  • Transform each pair into the absolute difference of their ASCII values.
  • Sum all the differences.
python
class Solution:
    def scoreOfString(self, s: str) -&gt; int:
        return sum(abs(a - b) for a, b in zip(s, s[1:]))

Complexity

  • Time: O(n), where n is the length of the string.
  • Space: O(1), assuming the stream operations do not create significant intermediate structures (or O(n) for the slice in Python).
  • Notes: While concise, this approach may have slight overhead compared to the raw iterative loop in some languages.

Pre-calculation

Intuition Convert the string into an array of integers representing ASCII values first, then perform the summation on this integer array.

Steps

  • Create an integer array nums where nums[i] is the ASCII value of s[i].
  • Iterate through nums to calculate the sum of absolute differences between adjacent elements.
python
class Solution:
    def scoreOfString(self, s: str) -&gt; int:
        nums = [ord(c) for c in s]
        score = 0
        for i in range(len(nums) - 1):
            score += abs(nums[i] - nums[i+1])
        return score

Complexity

  • Time: O(n), where n is the length of the string.
  • Space: O(n), to store the array of ASCII values.
  • Notes: This approach uses extra memory which is not strictly necessary for this problem.