At a conference, every attendee wears a badge printed with a single unique lowercase letter -- no two attendees share a letter. The string s records the original left-to-right queue order, and the string t records how the same attendees re-formed their queue after a break; both strings use exactly the same set of letters, each appearing exactly once. For every attendee, compute the absolute difference between their 0-indexed position in s and their 0-indexed position in t, then sum these differences across all attendees to obtain the total realignment cost.
s.t.A single integer: the total realignment cost.
s and t consist of lowercase English letters only.s is distinct, and t is a permutation of s (the same multiset of letters, each appearing exactly once in both).Example 1
Input
abc cab
Expected
4
Explanation
In s="abc": a is at position 0, b at 1, c at 2. In t="cab": c is at 0, a at 1, b at 2. Differences: a -> |0-1|=1, b -> |1-2|=1, c -> |2-0|=2. Total cost = 1+1+2 = 4.
Example 2
Input
a a
Expected
0
Explanation
There is only one attendee, letter 'a', which sits at position 0 in both queues, so the cost is |0-0|=0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →