Ctrl K

How Far the Letters Moved

Easy
You get two strings first and second. All letters in first are distinct, and second contains exactly the same letters, possibly in a different order. For each letter, find how many positions its index moved between first and second (the absolute difference of the indices). Return the sum of these moves over all letters.
Example 1
first="dog", second="god" → 4

d moves from 0 to 2, o stays at 1, g moves from 2 to 0: 2 + 0 + 2 = 4.

Example 2
first="plan", second="lpan" → 2

Only p and l swapped places, each moving by 1, for a total of 2.

Example 3
first="x", second="x" → 0
Constraints
  • 1 ≤ len(first) ≤ 26
  • first consists of distinct lowercase English letters
  • second is a rearrangement of first
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValue of a Roman Numeral EasySame Letter Cipher EasySame Letters, Reshuffled EasySame When Flipped EasyShuffle Into a Palindrome Easy
loading Python…
Input
first="dog", second="god"
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 70 tests on the server, hidden ones included.