Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

91 lines
1.8 KiB
Markdown

---
id: 6a22d77ddf034bc4e35b1d57
title: "Challenge 350: Letter Distance"
challengeType: 29
dashedName: challenge-350
---
# --description--
Given two strings of equal length, return the sum of the shortest distances between each pair of characters.
- The input will only contain lowercase letters
- The alphabet is treated as a circle, so the distance between `a` and `z` is 1.
# --hints--
`letter_distance("abc", "bcd")` should return `3`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(letter_distance("abc", "bcd"), 3)`)
}})
```
`letter_distance("abc", "xyz")` should return `9`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(letter_distance("abc", "xyz"), 9)`)
}})
```
`letter_distance("encrypt", "decrypt")` should return `10`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(letter_distance("encrypt", "decrypt"), 10)`)
}})
```
`letter_distance("algorithm", "codeblock")` should return `43`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(letter_distance("algorithm", "codeblock"), 43)`)
}})
```
`letter_distance("lobster", "penguin")` should return `47`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(letter_distance("lobster", "penguin"), 47)`)
}})
```
`letter_distance("alligator", "crocodile")` should return `55`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(letter_distance("alligator", "crocodile"), 55)`)
}})
```
# --seed--
## --seed-contents--
```py
def letter_distance(str1, str2):
return str1
```
# --solutions--
```py
def letter_distance(str1, str2):
total = 0
for a, b in zip(str1, str2):
diff = abs(ord(a) - ord(b))
total += min(diff, 26 - diff)
return total
```