Back to problems Sign in to unlock
Leetcode
Easy
Strings
Arrays
Math
Score Of A String
Compute the score of a string by summing the absolute differences of adjacent character codes.
Acceptance 0%
Problem Statement
Problem
Given a string s, define its score as the sum of the absolute differences between the ASCII values of every pair of adjacent characters.
For example, for s = "abc", the score is |a-b| + |b-c|.
Return the score of the string.
This is a straightforward character-by-character computation over the string.
Input Format
- A single string
s. - Characters are processed in order from left to right.
Output Format
- Return an integer representing the score of
s.
Constraints
- The string length is at least 1.
- The score is the sum of absolute differences of adjacent character codes.
- Character codes may be treated as ASCII/Unicode code points for the purpose of this problem, but standard lowercase-letter inputs are sufficient for practice.
Examples
Sample cases returned by the problem API.
Example 1
Input
s = "hello"
Output
13
Explanation
Adjacent differences are |'h'-'e'| = 3, |'e'-'l'| = 7, |'l'-'l'| = 0, and |'l'-'o'| = 3. The total is 13.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.