easyString 0 views

Score of a String

You are given a string s.

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.

Score of a String diagram

Example 1

Input: s = "hello"

Output: 13

Explanation: The ASCII values of the characters in s are: 'h' = 10^4 , 'e' = 10^1 , 'l' = 10^8 , 'o' = 111 . So, the score of s would be |10^4 - 10^1| + |10^1 - 10^8| + |10^8 - 10^8| + |10^8 - 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 . So, the score of s would be |122 - 97| + |97 - 122| = 25 + 25 = 50 .

Constraints

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

Hints

Companies

No companies reported yet.

Discussion

Sign in to join the discussion.

Loading discussion...

Test results

No test cases yet.