--- 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 ```