Skip to content

Practice Shift Distance Between Two Strings

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3361
LeetCode Problem

Shift Distance Between Two Strings

Problem Statement

Given equally long strings s and t, shift any character of s forward or backward by one alphabet position per operation, wrapping z to a or a to z. If its current alphabet index is j, a forward shift costs nextCost[j] and a backward shift costs previousCost[j]. Return the minimum total cost to transform s into t.

Source: Shift Distance Between Two Strings on LeetCode. Use the original task for its coding environment and submissions.

Examples

# 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

# 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

Constraints

1 <= s.length == t.length <= 10^5
s and t consist only of lowercase English letters.
nextCost.length == previousCost.length == 26
0 <= nextCost[i], previousCost[i] <= 10^9

Related LeetCode Problems

Practice more problems similar to Shift Distance Between Two Strings.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.