Score of a String - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
s = "hello"
›
Output:
13
💡 Note:
Adjacent differences: |'h'-'e'| = |104-101| = 3, |'e'-'l'| = |101-108| = 7, |'l'-'l'| = |108-108| = 0, |'l'-'o'| = |108-111| = 3. Sum = 3+7+0+3 = 13
Example 2 — Two Characters
$
Input:
s = "zab"
›
Output:
52
💡 Note:
|'z'-'a'| = |122-97| = 25, |'a'-'b'| = |97-98| = 1. Sum = 25+1 = 26. Wait, let me recalculate: |'z'-'a'| = 25, |'a'-'b'| = 1, so 25+1 = 26
Example 3 — Single Character
$
Input:
s = "a"
›
Output:
0
💡 Note:
Single character has no adjacent pairs, so score is 0
Constraints
- 1 ≤ s.length ≤ 100
- s consists of lowercase English letters only
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code