Show problems as
Classic problem #3110EasyString

Score of a String

Real-world version: The Roughness of a Strip of Letters
Tracked in this browser only
Read Score of a String on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A strip strip holds lowercase letters. Its roughness is the total, taken over every pair of neighbouring letters, of how far apart the two sit in the alphabet.

Return the roughness.

Examples

Example 1

Input
strip = "abc"
Output
2

The two neighbouring pairs each sit one letter apart, so the roughness is 2.

Example 2

Input
strip = "az"
Output
25

The two letters sit at opposite ends of the alphabet, twenty-five places apart.

Example 3

Input
strip = "aaaa"
Output
0

Every letter is the same, so no pair is any distance apart at all.

Constraints

  • 2 <= strip.length <= 100
  • strip holds only lowercase English letters

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def score_of_string(strip: str) -> int:
Java
public int scoreOfString(String strip)