3995. Minimum Cost to Convert String III
You are given two strings,
sourceandtarget.You are also given a 2D string array
rules, whererules[i] = [pattern_i, replacement_i], and an integer arraycosts, wherecosts[i]is the base cost of applyingrules[i]. Both arrays have the same length. Additionally,pattern_iandreplacement_ihave the same length.You may apply any rule any number of times. Each rule application works as follows:
- Choose an index
lsuch that the range of positions fromltol + pattern_i.length - 1exists in the current string and none of these positions has been used in a previous rule application.- For each index
j, the characterpattern_i[j]must either be equal to the current character at positionl + j, or be'*'.- Replace the characters in this range with
replacement_i. The replacement is used exactly as given and does not contain wildcards.- The cost of this rule application is
costs[i]plus the number of'*'characters inpattern_i.- Once a character position has been used in a rule application, it cannot be used in any later rule application.
Since every
pattern_iandreplacement_ihave the same length, character positions are preserved after every rule application.Return the minimum total cost required to transform
sourceintotarget. If it is impossible, return -1.
1 | class Solution { |