Minimum Cost to Convert String I — LeetCode 2976 Python Solution

MediumGraphArrayStringShortest Path
Problem
#2976
Reading time
5 min

The problem

You are given two 0-indexed strings source and target, both of length n and consisting of lowercase English letters. You are also given two 0-indexed character arrays original and changed, and an integer array cost, where cost[i] represents the cost of changing the character original[i] to the character changed[i].

Example

Input
source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20]
Output
28
Explanation
To convert the string "abcd" to string "acbe":

Python solution

Python
class Solution:
    def minimumCost(
        self,
        source: str,
        target: str,
        original: List[str],
        changed: List[str],
        cost: List[int],
    ) -> int:
        g = [[inf] * 26 for _ in range(26)]
        for i in range(26):
            g[i][i] = 0
        for x, y, z in zip(original, changed, cost):
            x = ord(x) - ord('a')
            y = ord(y) - ord('a')
            g[x][y] = min(g[x][y], z)
        for k in range(26):
            for i in range(26):
                for j in range(26):
                    g[i][j] = min(g[i][j], g[i][k] + g[k][j])
        ans = 0
        for a, b in zip(source, target):
            if a != b:
                x, y = ord(a) - ord('a'), ord(b) - ord('a')
                if g[x][y] >= inf:
                    return -1
                ans += g[x][y]
        return ans

Complexity

MeasureComplexity
TimeO(m + n + |\Sigma|^3)
SpaceO(|\Sigma|^2) auxiliary

Pattern: Depth-First Search

Follow one path to its end before trying the next — the default way to explore a graph. LeetCode 2976. Minimum Cost to Convert String I is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Graph.

The depth-first search guide has the Python template for the pattern and the 366 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2976. Minimum Cost to Convert String I?
LeetCode 2976. Minimum Cost to Convert String I is rated Medium on LeetCode.
What is the time complexity of LeetCode 2976. Minimum Cost to Convert String I?
The Python solution on this page runs in O(m + n + |\Sigma|^3).
What is the space complexity of LeetCode 2976. Minimum Cost to Convert String I?
The Python solution on this page uses O(|\Sigma|^2) auxiliary space.
What topics does LeetCode 2976. Minimum Cost to Convert String I cover?
LeetCode 2976. Minimum Cost to Convert String I is tagged Graph, Array, String and Shortest Path on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview