Practice Minimum Cost to Convert String I
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
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]. 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. 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. Note that there may exist indices i, j such that original[j] == original[i] and changed[j] == changed[i].
Source: Minimum Cost to Convert String I on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
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]
28
# Example 2
source = "aaaa", target = "bbbb", original = ["a","c"], changed = ["c","b"], cost = [1,2]
12
# Example 3
source = "abcd", target = "abce", original = ["a"], changed = ["e"], cost = [10000]
-1
Constraints
Related LeetCode Problems
Practice more problems similar to Minimum Cost to Convert String I.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.