The Minimum Cost to Convert String I problem gives you a source string and a target string of the same length. You are also given a set of transformation rules: you can change character a to b for a specific cost. You can apply any number of transformations (e.g., a -> b -> c). You need to find the minimum total cost to convert the entire source string into the target string.
This is a standard Minimum Cost to Convert String interview question at Google and Amazon. It is a "Shortest Path" problem hidden as a string problem. It tests if you can model the 26 lowercase letters as nodes in a graph and use the Floyd-Warshall Algorithm to find the minimum cost between any two characters.
The Floyd-Warshall interview pattern is the most effective.
source and target strings character by character, adding the precomputed cost to the total.Source: "abc", Target: "abd". Rules: 'c' to 'e' cost 1, 'e' to 'd' cost 2.
1 + 2 = 3.0 + 0 + 3 = 3.a -> c might be cheaper via a -> b -> c than a direct a -> c rule.Any time you have transformations between a small, fixed set of states (like characters 'a'-'z'), precompute an All-Pairs Shortest Path matrix. This makes subsequent lookups and significantly simplifies the main logic.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Minimum Cost to Convert String II | Hard | Solve | |
| Path with Maximum Probability | Medium | Solve | |
| Satisfiability of Equality Equations | Medium | Solve | |
| Minimum Cost of a Path With Special Roads | Medium | Solve | |
| Minimum Cost to Buy Apples | Medium | Solve |