Tap here to switch tabs
Problem
Submissions

Problem: Shift Distance Between Two Strings

Try to solve the Shift Distance Between Two Strings problem.

Statement

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

In one operation, you can transform a character at index i of s into:

  • The next character in alphabetical order, wrapping around from 'z' to 'a'. The cost of shifting character cc to its next character is nextCost[c], where cc represents the index of the character from 00 for 'a' to 2525 for 'z'.

  • The previous character in alphabetical order, wrapping around from 'a' to 'z'. The cost of shifting character cc to its previous character is previousCost[c], where cc represents the index of the character from 00 for 'a' to 2525 for 'z'.

The shift distance from s to t is the sum of the minimum costs required to convert each s[i] to t[i] for all valid indices i.

Return an integer representing the minimum shift distance to transform s into t.

Constraints:

  • 1≤1 \leq s.length == t.length ≤105\leq 10^5

  • s and t consist only of lowercase English letters.

  • nextCost.length == previousCost.length =26= 26

  • 0≤0 \leq nextCost[i], previousCost[i] ≤109\leq 10^9

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Shift Distance Between Two Strings

Try to solve the Shift Distance Between Two Strings problem.

Statement

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

In one operation, you can transform a character at index i of s into:

  • The next character in alphabetical order, wrapping around from 'z' to 'a'. The cost of shifting character cc to its next character is nextCost[c], where cc represents the index of the character from 00 for 'a' to 2525 for 'z'.

  • The previous character in alphabetical order, wrapping around from 'a' to 'z'. The cost of shifting character cc to its previous character is previousCost[c], where cc represents the index of the character from 00 for 'a' to 2525 for 'z'.

The shift distance from s to t is the sum of the minimum costs required to convert each s[i] to t[i] for all valid indices i.

Return an integer representing the minimum shift distance to transform s into t.

Constraints:

  • 1≤1 \leq s.length == t.length ≤105\leq 10^5

  • s and t consist only of lowercase English letters.

  • nextCost.length == previousCost.length =26= 26

  • 0≤0 \leq nextCost[i], previousCost[i] ≤109\leq 10^9