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

1.4 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
6a22d77ddf034bc4e35b1d57 Challenge 350: Letter Distance 28 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.

assert.equal(letterDistance("abc", "bcd"), 3);

letterDistance("abc", "xyz") should return 9.

assert.equal(letterDistance("abc", "xyz"), 9);

letterDistance("encrypt", "decrypt") should return 10.

assert.equal(letterDistance("encrypt", "decrypt"), 10);

letterDistance("algorithm", "codeblock") should return 43.

assert.equal(letterDistance("algorithm", "codeblock"), 43);

letterDistance("lobster", "penguin") should return 47.

assert.equal(letterDistance("lobster", "penguin"), 47);

letterDistance("alligator", "crocodile") should return 55.

assert.equal(letterDistance("alligator", "crocodile"), 55);

--seed--

--seed-contents--

function letterDistance(str1, str2) {

  return str1;
}

--solutions--

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);
}