|
| 1 | +3361\. Shift Distance Between Two Strings |
| 2 | + |
| 3 | +Medium |
| 4 | + |
| 5 | +You are given two strings `s` and `t` of the same length, and two integer arrays `nextCost` and `previousCost`. |
| 6 | + |
| 7 | +In one operation, you can pick any index `i` of `s`, and perform **either one** of the following actions: |
| 8 | + |
| 9 | +* Shift `s[i]` to the next letter in the alphabet. If `s[i] == 'z'`, you should replace it with `'a'`. This operation costs `nextCost[j]` where `j` is the index of `s[i]` in the alphabet. |
| 10 | +* Shift `s[i]` to the previous letter in the alphabet. If `s[i] == 'a'`, you should replace it with `'z'`. This operation costs `previousCost[j]` where `j` is the index of `s[i]` in the alphabet. |
| 11 | + |
| 12 | +The **shift distance** is the **minimum** total cost of operations required to transform `s` into `t`. |
| 13 | + |
| 14 | +Return the **shift distance** from `s` to `t`. |
| 15 | + |
| 16 | +**Example 1:** |
| 17 | + |
| 18 | +**Input:** s = "abab", t = "baba", nextCost = [100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], previousCost = [1,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] |
| 19 | + |
| 20 | +**Output:** 2 |
| 21 | + |
| 22 | +**Explanation:** |
| 23 | + |
| 24 | +* We choose index `i = 0` and shift `s[0]` 25 times to the previous character for a total cost of 1. |
| 25 | +* We choose index `i = 1` and shift `s[1]` 25 times to the next character for a total cost of 0. |
| 26 | +* We choose index `i = 2` and shift `s[2]` 25 times to the previous character for a total cost of 1. |
| 27 | +* We choose index `i = 3` and shift `s[3]` 25 times to the next character for a total cost of 0. |
| 28 | + |
| 29 | +**Example 2:** |
| 30 | + |
| 31 | +**Input:** s = "leet", t = "code", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] |
| 32 | + |
| 33 | +**Output:** 31 |
| 34 | + |
| 35 | +**Explanation:** |
| 36 | + |
| 37 | +* We choose index `i = 0` and shift `s[0]` 9 times to the previous character for a total cost of 9. |
| 38 | +* We choose index `i = 1` and shift `s[1]` 10 times to the next character for a total cost of 10. |
| 39 | +* We choose index `i = 2` and shift `s[2]` 1 time to the previous character for a total cost of 1. |
| 40 | +* We choose index `i = 3` and shift `s[3]` 11 times to the next character for a total cost of 11. |
| 41 | + |
| 42 | +**Constraints:** |
| 43 | + |
| 44 | +* <code>1 <= s.length == t.length <= 10<sup>5</sup></code> |
| 45 | +* `s` and `t` consist only of lowercase English letters. |
| 46 | +* `nextCost.length == previousCost.length == 26` |
| 47 | +* <code>0 <= nextCost[i], previousCost[i] <= 10<sup>9</sup></code> |
0 commit comments