Class Solution
-
- All Implemented Interfaces:
public final class Solution
3361 - Shift Distance Between Two Strings.
Medium
You are given two strings
s
andt
of the same length, and two integer arraysnextCost
andpreviousCost
.In one operation, you can pick any index
i
ofs
, and perform either one of the following actions:Shift
s[i]
to the next letter in the alphabet. Ifs[i] == 'z'
, you should replace it with'a'
. This operation costsnextCost[j]
wherej
is the index ofs[i]
in the alphabet.Shift
s[i]
to the previous letter in the alphabet. Ifs[i] == 'a'
, you should replace it with'z'
. This operation costspreviousCost[j]
wherej
is the index ofs[i]
in the alphabet.
The shift distance is the minimum total cost of operations required to transform
s
intot
.Return the shift distance from
s
tot
.Example 1:
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
Output: 2
Explanation:
We choose index
i = 0
and shifts[0]
25 times to the previous character for a total cost of 1.We choose index
i = 1
and shifts[1]
25 times to the next character for a total cost of 0.We choose index
i = 2
and shifts[2]
25 times to the previous character for a total cost of 1.We choose index
i = 3
and shifts[3]
25 times to the next character for a total cost of 0.
Example 2:
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
Output: 31
Explanation:
We choose index
i = 0
and shifts[0]
9 times to the previous character for a total cost of 9.We choose index
i = 1
and shifts[1]
10 times to the next character for a total cost of 10.We choose index
i = 2
and shifts[2]
1 time to the previous character for a total cost of 1.We choose index
i = 3
and shifts[3]
11 times to the next character for a total cost of 11.
Constraints:
<code>1 <= s.length == t.length <= 10<sup>5</sup></code>
s
andt
consist only of lowercase English letters.nextCost.length == previousCost.length == 26
<code>0 <= nextCosti, previousCosti<= 10<sup>9</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-