Score of a String

You are given a string `s`. The score of a string is the sum of the absolute differences between the ASCII values of every pair of adjacent characters. Return the score of `s`.

30:00

Score of a String
easy
Free written solution
Topics
Companies

You are given a string s. The score of a string is the sum of the absolute differences between the ASCII values of every pair of adjacent characters. Return the score of s.

Example 1:
Input: {"s":"hello"}
Output: 13
Constraints:
  • 2≤s.length≤1002 \leq s.\text{length} \leq 100

  • s consists only of lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"hello"}

Place every letter of "hello" on the a..z ruler. The score is the total distance travelled hopping from each letter to the next.

s, pair (i-1, i)

h

0

e

1

l

2

l

3

o

4

hops on the a..z ruler (distance = ASCII difference)

a97b98c99d100e101f102g103h104i105j106k107l108m109n110o111p112q113r114s115t116u117v118w119x120y121z122

score = sum of hop lengths

score = 0

Variables
VariableValue
score0
DepthFunction Call
Stack empty
0/6