Shift Distance Between Two Strings - Problem

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. 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.
  • 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.

The shift distance is the minimum total cost of operations required to transform s into t.

Return the shift distance from s to t.

Input & Output

Example 1 — Basic Transformation
$ Input: s = "abc", t = "bcd", nextCost = [1,1,1,1,...], previousCost = [1,1,1,1,...]
Output: 3
💡 Note: Transform 'a'→'b' (cost 1), 'b'→'c' (cost 1), 'c'→'d' (cost 1). Total cost = 3
Example 2 — Wraparound Case
$ Input: s = "za", t = "ab", nextCost = [1,1,1,...], previousCost = [2,2,2,...]
Output: 2
💡 Note: Transform 'z'→'a' (cost 1 forward), 'a'→'b' (cost 1 forward). Total cost = 2
Example 3 — Choose Backward Path
$ Input: s = "ab", t = "ba", nextCost = [10,10,...], previousCost = [1,1,...]
Output: 11
💡 Note: For 'a'→'b': forward path costs 10, backward path costs 25, choose forward (10). For 'b'→'a': forward path costs 250, backward path costs 1, choose backward (1). Total cost = 11

Constraints

  • 1 ≤ s.length == t.length ≤ 105
  • s and t consist only of lowercase English letters
  • nextCost.length == previousCost.length == 26
  • 1 ≤ nextCost[i], previousCost[i] ≤ 106

Visualization

Tap to expand
Shift Distance Between Two Strings INPUT s = "abc" a b c t = "bcd" b c d Circular Alphabet: a b c d ... z ... nextCost = [1,1,1,...] prevCost = [1,1,1,...] ALGORITHM STEPS 1 For each position i Compare s[i] with t[i] 2 Calculate distances Forward: (t-s+26)%26 Backward: (s-t+26)%26 3 Compute costs Sum nextCost for forward Sum prevCost for backward 4 Choose minimum min(forward, backward) Example: a-->b: fwd=1, bwd=25 min(1,25)=1 b-->c: fwd=1, bwd=25 min(1,25)=1 c-->d: fwd=1, bwd=25 min(1,25)=1 FINAL RESULT Transformation: a --> b cost: 1 b --> c cost: 1 c --> d cost: 1 Total Cost: 1 + 1 + 1 = 3 Output 3 OK - Minimum cost found! Key Insight: For each character, we can shift forward (next) or backward (previous) around the circular alphabet. The optimal choice at each position is independent - compute both directions and pick the cheaper one. Time: O(n * 26) where n = string length. Space: O(1) for prefix sums or O(26) with precomputation. TutorialsPoint - Shift Distance Between Two Strings | Optimal Solution
Asked in
Google 35 Microsoft 28 Amazon 22
34.6K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen