mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
64 lines
5.5 KiB
HTML
64 lines
5.5 KiB
HTML
<p>You are given two <strong>0-indexed</strong> strings <code>source</code> and <code>target</code>, both of length <code>n</code> and consisting of <strong>lowercase</strong> English characters. You are also given two <strong>0-indexed</strong> string arrays <code>original</code> and <code>changed</code>, and an integer array <code>cost</code>, where <code>cost[i]</code> represents the cost of converting the string <code>original[i]</code> to the string <code>changed[i]</code>.</p>
|
|
|
|
<p>You start with the string <code>source</code>. In one operation, you can pick a <strong>substring</strong> <code>x</code> from the string, and change it to <code>y</code> at a cost of <code>z</code> <strong>if</strong> there exists <strong>any</strong> index <code>j</code> such that <code>cost[j] == z</code>, <code>original[j] == x</code>, and <code>changed[j] == y</code>. You are allowed to do <strong>any</strong> number of operations, but any pair of operations must satisfy <strong>either</strong> of these two conditions:</p>
|
|
|
|
<ul>
|
|
<li>The substrings picked in the operations are <code>source[a..b]</code> and <code>source[c..d]</code> with either <code>b < c</code> <strong>or</strong> <code>d < a</code>. In other words, the indices picked in both operations are <strong>disjoint</strong>.</li>
|
|
<li>The substrings picked in the operations are <code>source[a..b]</code> and <code>source[c..d]</code> with <code>a == c</code> <strong>and</strong> <code>b == d</code>. In other words, the indices picked in both operations are <strong>identical</strong>.</li>
|
|
</ul>
|
|
|
|
<p>Return <em>the <strong>minimum</strong> cost to convert the string </em><code>source</code><em> to the string </em><code>target</code><em> using <strong>any</strong> number of operations</em>. <em>If it is impossible to convert</em> <code>source</code> <em>to</em> <code>target</code>,<em> return</em> <code>-1</code>.</p>
|
|
|
|
<p><strong>Note</strong> that there may exist indices <code>i</code>, <code>j</code> such that <code>original[j] == original[i]</code> and <code>changed[j] == changed[i]</code>.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> 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]
|
|
<strong>Output:</strong> 28
|
|
<strong>Explanation:</strong> To convert "abcd" to "acbe", do the following operations:
|
|
- Change substring source[1..1] from "b" to "c" at a cost of 5.
|
|
- Change substring source[2..2] from "c" to "e" at a cost of 1.
|
|
- Change substring source[2..2] from "e" to "b" at a cost of 2.
|
|
- Change substring source[3..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.
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> source = "abcdefgh", target = "acdeeghh", original = ["bcd","fgh","thh"], changed = ["cde","thh","ghh"], cost = [1,3,5]
|
|
<strong>Output:</strong> 9
|
|
<strong>Explanation:</strong> To convert "abcdefgh" to "acdeeghh", do the following operations:
|
|
- Change substring source[1..3] from "bcd" to "cde" at a cost of 1.
|
|
- Change substring source[5..7] from "fgh" to "thh" at a cost of 3. We can do this operation because indices [5,7] are disjoint with indices picked in the first operation.
|
|
- Change substring source[5..7] from "thh" to "ghh" at a cost of 5. We can do this operation because indices [5,7] are disjoint with indices picked in the first operation, and identical with indices picked in the second operation.
|
|
The total cost incurred is 1 + 3 + 5 = 9.
|
|
It can be shown that this is the minimum possible cost.
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 3:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> source = "abcdefgh", target = "addddddd", original = ["bcd","defgh"], changed = ["ddd","ddddd"], cost = [100,1578]
|
|
<strong>Output:</strong> -1
|
|
<strong>Explanation:</strong> It is impossible to convert "abcdefgh" to "addddddd".
|
|
If you select substring source[1..3] as the first operation to change "abcdefgh" to "adddefgh", you cannot select substring source[3..7] as the second operation because it has a common index, 3, with the first operation.
|
|
If you select substring source[3..7] as the first operation to change "abcdefgh" to "abcddddd", you cannot select substring source[1..3] as the second operation because it has a common index, 3, with the first operation.
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= source.length == target.length <= 1000</code></li>
|
|
<li><code>source</code>, <code>target</code> consist only of lowercase English characters.</li>
|
|
<li><code>1 <= cost.length == original.length == changed.length <= 100</code></li>
|
|
<li><code>1 <= original[i].length == changed[i].length <= source.length</code></li>
|
|
<li><code>original[i]</code>, <code>changed[i]</code> consist only of lowercase English characters.</li>
|
|
<li><code>original[i] != changed[i]</code></li>
|
|
<li><code>1 <= cost[i] <= 10<sup>6</sup></code></li>
|
|
</ul>
|