Classic problem #3110EasyString
Score of a String
Real-world version: The Roughness of a Strip of LettersTracked 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)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's