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
74 lines
1.4 KiB
Markdown
74 lines
1.4 KiB
Markdown
---
|
|
id: 6a22d77ddf034bc4e35b1d57
|
|
title: "Challenge 350: Letter Distance"
|
|
challengeType: 28
|
|
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--
|
|
|
|
`letterDistance("abc", "bcd")` should return `3`.
|
|
|
|
```js
|
|
assert.equal(letterDistance("abc", "bcd"), 3);
|
|
```
|
|
|
|
`letterDistance("abc", "xyz")` should return `9`.
|
|
|
|
```js
|
|
assert.equal(letterDistance("abc", "xyz"), 9);
|
|
```
|
|
|
|
`letterDistance("encrypt", "decrypt")` should return `10`.
|
|
|
|
```js
|
|
assert.equal(letterDistance("encrypt", "decrypt"), 10);
|
|
```
|
|
|
|
`letterDistance("algorithm", "codeblock")` should return `43`.
|
|
|
|
```js
|
|
assert.equal(letterDistance("algorithm", "codeblock"), 43);
|
|
```
|
|
|
|
`letterDistance("lobster", "penguin")` should return `47`.
|
|
|
|
```js
|
|
assert.equal(letterDistance("lobster", "penguin"), 47);
|
|
```
|
|
|
|
`letterDistance("alligator", "crocodile")` should return `55`.
|
|
|
|
```js
|
|
assert.equal(letterDistance("alligator", "crocodile"), 55);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
function letterDistance(str1, str2) {
|
|
|
|
return str1;
|
|
}
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
function letterDistance(str1, str2) {
|
|
return str1.split("").reduce((sum, char, i) => {
|
|
const diff = Math.abs(char.charCodeAt(0) - str2[i].charCodeAt(0));
|
|
return sum + Math.min(diff, 26 - diff);
|
|
}, 0);
|
|
}
|
|
```
|