#3361

Shift Distance Between Two Strings

specialist · 845 · lc medium +31 · 53.5% accepted · 68 likes · top 45%

Description

You are given two strings s and t of equal length, along with two integer arrays nextCost and previousCost.

In each operation, pick any index i in s and perform one of:

- Advance s[i] to the next letter (wrapping 'z' to 'a'); this costs nextCost[j] where j is the alphabet index of s[i].

- Move s[i] to the previous letter (wrapping 'a' to 'z'); this costs previousCost[j] where j is the alphabet index of s[i].

The shift distance is the minimum total cost to transform s into t.

Return the shift distance from s to t.

Code

1
2
3