10038. Minimum Cost to Convert String II
ou are given two 0-indexed strings
sourceandtarget, both of lengthnand consisting of lowercase English characters. You are also given two 0-indexed string arraysoriginalandchanged, and an integer arraycost, wherecost[i]represents the cost of converting the stringoriginal[i]to the stringchanged[i].You start with the string
source. In one operation, you can pick a substringxfrom the string, and change it toyat a cost ofzif there exists any indexjsuch thatcost[j] == z,original[j] == x, andchanged[j] == y. You are allowed to do any number of operations, but any pair of operations must satisfy either of these two conditions:
- The substrings picked in the operations are
source[a..b]andsource[c..d]with eitherb < cord < a. In other words, the indices picked in both operations are disjoint.- The substrings picked in the operations are
source[a..b]andsource[c..d]witha == candb == d. In other words, the indices picked in both operations are identical.Return the minimum cost to convert the string
sourceto the stringtargetusing any number of operations. If it is impossible to convertsourcetotarget, return-1.Note that there may exist indices
i,jsuch thatoriginal[j] == original[i]andchanged[j] == changed[i].