Class Solution
-
- All Implemented Interfaces:
public final class Solution2976 - Minimum Cost to Convert String I.
Medium
You are given two 0-indexed strings
sourceandtarget, both of lengthnand consisting of lowercase English letters. You are also given two 0-indexed character arraysoriginalandchanged, and an integer arraycost, wherecost[i]represents the cost of changing the characteroriginal[i]to the characterchanged[i].You start with the string
source. In one operation, you can pick a characterxfrom the string and change it to the characteryat a cost ofzif there exists any indexjsuch thatcost[j] == z,original[j] == x, andchanged[j] == y.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].Example 1:
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":
Change value at index 1 from 'b' to 'c' at a cost of 5.
Change value at index 2 from 'c' to 'e' at a cost of 1.
Change value at index 2 from 'e' to 'b' at a cost of 2.
Change value at index 3 from 'd' to 'e' at a cost of 20.
The total cost incurred is 5 + 1 + 2 + 20 = 28.
It can be shown that this is the minimum possible cost.
Example 2:
Input: source = "aaaa", target = "bbbb", original = "a","c", changed = "c","b", cost = 1,2
Output: 12
Explanation: To change the character 'a' to 'b' change the character 'a' to 'c' at a cost of 1, followed by changing the character 'c' to 'b' at a cost of 2, for a total cost of 1 + 2 = 3. To change all occurrences of 'a' to 'b', a total cost of 3 * 4 = 12 is incurred.
Example 3:
Input: source = "abcd", target = "abce", original = "a", changed = "e", cost = 10000
Output: -1
Explanation: It is impossible to convert source to target because the value at index 3 cannot be changed from 'd' to 'e'.
Constraints:
<code>1 <= source.length == target.length <= 10<sup>5</sup></code>
source,targetconsist of lowercase English letters.1 <= cost.length == original.length == changed.length <= 2000original[i],changed[i]are lowercase English letters.<code>1 <= costi<= 10<sup>6</sup></code>
original[i] != changed[i]