2976. Minimum Cost to Convert String I Medium
1/**
2 * [2976] Minimum Cost to Convert String I
3 *
4 * You are given two 0-indexed strings source and target, both of length n and consisting of lowercase English letters. You are also given two 0-indexed character arrays original and changed, and an integer array cost, where cost[i] represents the cost of changing the character original[i] to the character changed[i].
5 * You start with the string source. In one operation, you can pick a character x from the string and change it to the character y at a cost of z if there exists any index j such that cost[j] == z, original[j] == x, and changed[j] == y.
6 * Return the minimum cost to convert the string source to the string target using any number of operations. If it is impossible to convert source to target, return -1.
7 * Note that there may exist indices i, j such that original[j] == original[i] and changed[j] == changed[i].
8 *
9 * <strong class="example">Example 1:
10 *
11 * 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]
12 * Output: 28
13 * Explanation: To convert the string "abcd" to string "acbe":
14 * - Change value at index 1 from 'b' to 'c' at a cost of 5.
15 * - Change value at index 2 from 'c' to 'e' at a cost of 1.
16 * - Change value at index 2 from 'e' to 'b' at a cost of 2.
17 * - Change value at index 3 from 'd' to 'e' at a cost of 20.
18 * The total cost incurred is 5 + 1 + 2 + 20 = 28.
19 * It can be shown that this is the minimum possible cost.
20 *
21 * <strong class="example">Example 2:
22 *
23 * Input: source = "aaaa", target = "bbbb", original = ["a","c"], changed = ["c","b"], cost = [1,2]
24 * Output: 12
25 * 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.
26 *
27 * <strong class="example">Example 3:
28 *
29 * Input: source = "abcd", target = "abce", original = ["a"], changed = ["e"], cost = [10000]
30 * Output: -1
31 * Explanation: It is impossible to convert source to target because the value at index 3 cannot be changed from 'd' to 'e'.
32 *
33 *
34 * Constraints:
35 *
36 * 1 <= source.length == target.length <= 10^5
37 * source, target consist of lowercase English letters.
38 * 1 <= cost.length == original.length == changed.length <= 2000
39 * original[i], changed[i] are lowercase English letters.
40 * 1 <= cost[i] <= 10^6
41 * original[i] != changed[i]
42 *
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/minimum-cost-to-convert-string-i/
47// discuss: https://leetcode.com/problems/minimum-cost-to-convert-string-i/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52 pub fn minimum_cost(source: String, target: String, original: Vec<char>, changed: Vec<char>, cost: Vec<i32>) -> i64 {
53
54 }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn test_2976() {
65 }
66}
67
Back
© 2025 bowen.ge All Rights Reserved.