Shift Distance Between Two Strings Problem
Description
LeetCode Problem 3361.
You are given two strings s and t of the same length, and two integer arrays nextCost and previousCost.
In one operation, you can pick any index i of s, 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]wherejis 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]wherejis the index ofs[i]in the alphabet.
The shift distance is the minimum total cost of operations required to transform s into t.
Return the shift distance from s to t.
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 shift s[0] 25 times to the previous character for a total cost of 1.
- We choose index i = 1 and shift s[1] 25 times to the next character for a total cost of 0.
- We choose index i = 2 and shift s[2] 25 times to the previous character for a total cost of 1.
- We choose index i = 3 and shift s[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 shift s[0] 9 times to the previous character for a total cost of 9.
- We choose index i = 1 and shift s[1] 10 times to the next character for a total cost of 10.
- We choose index i = 2 and shift s[2] 1 time to the previous character for a total cost of 1.
- We choose index i = 3 and shift s[3] 11 times to the next character for a total cost of 11.
Constraints:
1 <= s.length == t.length <= 10^5sandtconsist only of lowercase English letters.nextCost.length == previousCost.length == 260 <= nextCost[i], previousCost[i] <= 10^9
Solution
Approach: Prefix Sums on a Circle
Each letter can be shifted forward at cost nextCost[c] or backward at cost previousCost[c], and the alphabet is circular. For every position the two strings give a letter x and a target y.
Double the cost arrays so that a circular arc becomes a plain range, then build prefix sums. The forward cost from x to y is next[y'] - next[x] with y' = y + 26 if y < x; the backward cost is symmetric using previousCost. Add the cheaper of the two for every position.
Complexity: O(n + 26) time and O(26) space, where n is the length of the strings.
class Solution:
def shiftDistance(
self, s: str, t: str, nextCost: List[int], previousCost: List[int]
) -> int:
m = 26
s1 = [0] * (m << 1 | 1)
s2 = [0] * (m << 1 | 1)
for i in range(m << 1):
s1[i + 1] = s1[i] + nextCost[i % m]
s2[i + 1] = s2[i] + previousCost[(i + 1) % m]
ans = 0
for a, b in zip(s, t):
x, y = ord(a) - ord("a"), ord(b) - ord("a")
c1 = s1[y + m if y < x else y] - s1[x]
c2 = s2[x + m if x < y else x] - s2[y]
ans += min(c1, c2)
return ans