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
Score of a String: Sum Adjacent ASCII Differencesh104e101l108l108o111|104-101| = 3|101-108| = 7|108-108| = 0|108-111| = 3Sum = 3 + 7 + 0 + 3 = 13Final Score: 13
Asked in
Google 15 Microsoft 12 Amazon 8
12.0K Views
Medium Frequency
~5 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen