Minimum Cost to Convert String II is a more complex version where transformations aren't just single characters, but entire substrings. You are given a set of original strings and their corresponding converted strings with a cost. You want to convert a source string to a target string by replacing non-overlapping substrings.
This is a high-difficulty Minimum Cost to Convert String interview question at Atlassian and Amazon. it combines Floyd-Warshall (for the transformation costs) with Dynamic Programming and Tries (to efficiently find valid substring matches). It tests a candidate's ability to integrate multiple data structures into a single solution.
The DP with Trie and Floyd-Warshall interview pattern is required.
original and converted substrings.dp[i] is the minimum cost to convert the prefix of length i.i, look for all substrings source[j...i] that match an entry in your Trie and update dp[i] accordingly.Source: "abcdef", Target: "abcxyz". Rule: "def" -> "xyz" cost 10.
dp[0...3] cost 0 (since they already match).i=6, we find the substring "def" matches the target "xyz".dp[6] = min(dp[6], dp[3] + cost("def" -> "xyz")) = 10.source[i] == target[i]).This problem is essentially "Segmenting a string optimally." Whenever you can break a string into pieces with associated costs, it's a Linear DP problem. The difficulty here is just in calculating the costs and finding the segments.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Minimum Cost to Convert String I | Medium | Solve | |
| Concatenated Words | Hard | Solve | |
| Extra Characters in a String | Medium | Solve | |
| Minimum Cost to Reach Destination in Time | Hard | Solve | |
| Number of Ways to Form a Target String Given a Dictionary | Hard | Solve |