Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Shift Distance Between Two Strings

Updated
4 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "3361" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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. 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 <= 105 s and t consist only of lowercase English letters. nextCost.length == previousCost.length == 26 0 <= nextCost[i], previousCost[i] <= 109

Explanation

Here's an efficient solution to the problem:

  • Calculate the difference: For each character position, find the absolute difference between the ASCII values of the characters in s and t. This represents the "distance" between the characters.
  • Minimize cost: For each character difference, determine the minimum cost to shift from s[i] to t[i] by either shifting forward or backward, using the provided nextCost and previousCost arrays.
  • Accumulate total cost: Sum up the minimum costs for all character positions to get the total shift distance.

  • Runtime Complexity: O(N), where N is the length of the strings s and t.

  • Storage Complexity: O(1)

Code

    def shift_distance(s: str, t: str, nextCost: list[int], previousCost: list[int]) -> int:
    """
    Calculates the minimum shift distance to transform string s into string t.

    Args:
        s: The starting string.
        t: The target string.
        nextCost: The cost of shifting to the next letter in the alphabet.
        previousCost: The cost of shifting to the previous letter in the alphabet.

    Returns:
        The minimum shift distance.
    """
    n = len(s)
    total_cost = 0

    for i in range(n):
        start_char = s[i]
        target_char = t[i]

        start_index = ord(start_char) - ord('a')
        target_index = ord(target_char) - ord('a')

        diff = abs(start_index - target_index)

        forward_cost = 0
        backward_cost = 0

        if start_index <= target_index:
            for j in range(start_index, target_index):
                forward_cost += nextCost[j]
        else:
            for j in range(target_index, start_index):
                backward_cost += previousCost[j]

        forward_cost_wrap = 0
        backward_cost_wrap = 0

        if start_index <= target_index:
            for j in range(target_index, start_index + 26):
                wrapped_index = j % 26
                backward_cost_wrap += previousCost[wrapped_index]
        else:
             for j in range(start_index, target_index + 26):
                wrapped_index = j % 26
                forward_cost_wrap += nextCost[wrapped_index]

        total_cost += min(forward_cost, backward_cost)

    return total_cost

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Shift Distance Between Two Strings