Problem Description
Given two strings, source and target, of equal length and consisting of lowercase letters, you are allowed to perform a series of transformations. For each allowed transformation provided as parallel arrays (original, changed) with associated cost values, you can change a character from the string if there exists a corresponding transformation (or chain of transformations) that takes you from one letter to another. The goal is to determine the minimum total cost required to convert source into target, applying any number of these operations. If it is impossible to convert the string, return -1.
Key Insights
- Model the allowed transformations as a directed weighted graph where nodes represent letters.
- For each allowed transformation, update the edge with the minimum cost if multiple operations exist for the same pair.
- Use the Floyd Warshall algorithm to compute the minimum conversion cost between every pair of letters.
- For each position in the source string, if the minimal cost to convert the source letter to the target letter is not available (infinite), the conversion is impossible.
- Sum up the conversion cost for each character position, applying the transformation cost computed via the graph.
Space and Time Complexity
Time Complexity: O(26^3 + n), where n is the length of the strings. The 26^3 part comes from the Floyd Warshall algorithm on 26 English letters. Space Complexity: O(26^2) for the cost matrix, plus O(n) for input strings.
Solution
The problem can be solved by first modeling the transformations as a graph where each node is a letter ('a'-'z'). Every allowed operation creates a directed edge from letter x to letter y with a weight equal to the operation's cost. If there are multiple operations between the same pair, we take the one with the smallest cost.
After constructing the graph, we apply the Floyd Warshall algorithm to compute the minimum cost path between every pair of letters. This gives us the cheapest way to transform any letter into any other letter (possibly via intermediate transformations).
Finally, we iterate over each index of the source string. For each character, if the source and target letters are the same, no cost is incurred. Otherwise, we add the minimum cost to transform the source letter to the target letter based on our computed graph. If any transformation is impossible (i.e., remains infinite), we return -1.